Class 作业:比赛时间排序器
Class assignment: race time sorter
好的,这是作业:
You're a swimmer, and you want to compare all of your race times to find the >fastest one. Write a program that continuously takes race times as doubles >from standard input, until the input is "no more races," at which point it >should print out the time of your fastest race.
相当简单,我是这么认为的。通过一些工作,我想出了以下内容:
race_time = input("")
list1=[]
while race_time != ("no more races"):
list1.append("")
if race_time == ("no more races"):
print(min(list1))
好的,请征求意见。只要 race_time 中的输入不等于 "no more races",您下次输入时,该值就会附加到 list1。一旦 race_time 等于 "no more races",打印 list1 中的最小值。
直截了当吧?
好吧,我将该代码插入到 MyProgrammingLab 中并提交了。它给了我以下... 'feedback'
Your code did not work as expected.
More Hints:
⇒ We think you might want to consider using: float
⇒ Solutions with your approach don't usually use: ==
⇒ Solutions with your approach don't usually use: if
好的...那么我为什么要使用那些?
无论如何,所以我把代码扔进 IDLE 并 运行 它。在这里我得到以下错误:
Traceback (most recent call last):
File "C:\Users\F2-233\Desktop\times.py", line 4, in
list1.append("")
MemoryError
那么...什么是追溯?这本书没有谈到这个问题,在网上搜索时,我找到了很多代码示例来解决这个问题,但没有找到对问题所在的解释。
首先,回答关于 Traceback 的问题:"traceback" 是指向代码中导致代码在 运行 期间中断的位置的映射时间.
您看到此消息的原因是您在 Traceback 中描述的行超出了应用程序的可用内存:list1.append("")
在这种情况下,需要注意的是race_time = input("")
只会执行一次(当race_time
被初始化时)。它不会在您每次引用 race_time
.
时执行
虽然这不会为您完成任务,但它会为您指明一个正确的方向:
race_time = input("")
list1=[]
while race_time != ("no more races"):
list1.append("")
race_time = input("") # prompt for another race time
if race_time == ("no more races"):
print(min(list1))
您的想法是对的,但我发现您的解决方案存在 2 个问题。
输入提示在循环外,这意味着用户只会被要求输入一次比赛时间。
用户输入保存为字符串,要进行值比较,它必须是浮点数(python 中的双精度)。
结果代码是:
list1 = []
while True:
race_time = input()
if race_time == 'no more races':
break
list1.append(float(race_time))
print(min(list1))
"race time" 输入的格式是什么? (例如,是不是类似于“3.456”秒)
无论如何,这里有一段代码会不断要求用户输入,直到给出 "no more races",在这种情况下,程序会退出:
def race_time():
list1 = []
while True:
race_time = input("")
if race_time == ("no more races"):
print("min race time: {0}".format(min(list1)))
break
else:
list1.append(float(race_time))
好的,这是作业:
You're a swimmer, and you want to compare all of your race times to find the >fastest one. Write a program that continuously takes race times as doubles >from standard input, until the input is "no more races," at which point it >should print out the time of your fastest race.
相当简单,我是这么认为的。通过一些工作,我想出了以下内容:
race_time = input("")
list1=[]
while race_time != ("no more races"):
list1.append("")
if race_time == ("no more races"):
print(min(list1))
好的,请征求意见。只要 race_time 中的输入不等于 "no more races",您下次输入时,该值就会附加到 list1。一旦 race_time 等于 "no more races",打印 list1 中的最小值。
直截了当吧?
好吧,我将该代码插入到 MyProgrammingLab 中并提交了。它给了我以下... 'feedback'
Your code did not work as expected. More Hints: ⇒ We think you might want to consider using: float ⇒ Solutions with your approach don't usually use: == ⇒ Solutions with your approach don't usually use: if
好的...那么我为什么要使用那些?
无论如何,所以我把代码扔进 IDLE 并 运行 它。在这里我得到以下错误:
Traceback (most recent call last): File "C:\Users\F2-233\Desktop\times.py", line 4, in list1.append("") MemoryError
那么...什么是追溯?这本书没有谈到这个问题,在网上搜索时,我找到了很多代码示例来解决这个问题,但没有找到对问题所在的解释。
首先,回答关于 Traceback 的问题:"traceback" 是指向代码中导致代码在 运行 期间中断的位置的映射时间.
您看到此消息的原因是您在 Traceback 中描述的行超出了应用程序的可用内存:list1.append("")
在这种情况下,需要注意的是race_time = input("")
只会执行一次(当race_time
被初始化时)。它不会在您每次引用 race_time
.
虽然这不会为您完成任务,但它会为您指明一个正确的方向:
race_time = input("")
list1=[]
while race_time != ("no more races"):
list1.append("")
race_time = input("") # prompt for another race time
if race_time == ("no more races"):
print(min(list1))
您的想法是对的,但我发现您的解决方案存在 2 个问题。
输入提示在循环外,这意味着用户只会被要求输入一次比赛时间。
用户输入保存为字符串,要进行值比较,它必须是浮点数(python 中的双精度)。
结果代码是:
list1 = []
while True:
race_time = input()
if race_time == 'no more races':
break
list1.append(float(race_time))
print(min(list1))
"race time" 输入的格式是什么? (例如,是不是类似于“3.456”秒)
无论如何,这里有一段代码会不断要求用户输入,直到给出 "no more races",在这种情况下,程序会退出:
def race_time():
list1 = []
while True:
race_time = input("")
if race_time == ("no more races"):
print("min race time: {0}".format(min(list1)))
break
else:
list1.append(float(race_time))