2017-10-05 80 views
0

好了,所以這裏是賦值:課堂作業:比賽時間,分揀

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中的輸入不等於「沒有更多的比賽」,您就輸入下一次,並將該值附加到list1。一旦race_time等於「不再有比賽」,則從list1打印最小值。

直截了當吧?

嗯,我毫不猶豫地掏出代碼爲MyProgrammingLab並提交。它給了我以下...'反饋'

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

好吧......那我爲什麼要用這些?

無論如何,所以我扔的代碼進入空閒並運行它。這裏我得到以下錯誤:

Traceback (most recent call last): File "C:\Users\F2-233\Desktop\times.py", line 4, in list1.append("") MemoryError

那麼...什麼是追溯?這本書還沒有談到這一點,並在網上搜索,我發現很多的代碼示例來解決這一問題,但沒有發現的問題是什麼的解釋。

回答

0

什麼是一個 「比賽時間」 輸入格式? (例如它是否像「3。456" 秒)

在任何情況下,這裏的連續請求輸入用戶直到代碼‘無多個種族’被給予,在這種情況下,程序退出:

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)) 
0

首先,要回答你的問題有關回溯:A「回溯」是一張地圖,導致它在運行時打破你的代碼的位置。

你看到這個的原因是因爲你超出可用內存爲您在回溯中描述的網上申請: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)) 
+0

是它的內存限制我在代碼中實現或者是一個系統限制? –

+0

你的上限取決於您的環境=>操作系統,Python運行時(Jython中,activatestate,純Python等),32位VS 64位進程等 – axlj

0

你有正確的想法,但我看到你解決2個問題。

  1. 輸入提示在循環之外,這意味着用戶只會被要求比賽時間一次。

  2. 用戶輸入保存爲字符串,要做的值比較它必須是一個浮子(在python雙)。

生成的代碼是:

list1 = [] 

while True: 
    race_time = input() 
    if race_time == 'no more races': 
     break 
    list1.append(float(race_time)) 

print(min(list1)) 
+0

*嘆*此是的原因,我真的很憎恨MyProgrammingLab。我採取這一代碼,將其放到空閒,做工精細的。直到我說:「沒有更多的比賽」,那麼它顯示的最低酷似它應該注意到的數字。我把代碼放到MPL我得到了「我沒有沒有看到使用浮動的解決方案「。我把飄出來,重新提交它,我得到「你真的應該考慮使用浮動」 我的上帝,這是加重!但是... MPL是我必須安撫我的成績。 –

+0

@MichaelCividanes解決與任何語言相同的問題有很多方法。 MyProgrammingLab可能會鼓勵您使用他們認爲是最佳實踐的特定技術。不要灰心,不要慌張。 – axlj