2016-01-20 45 views
1

我正在使用Python 3,並且在運行此文件時有一點問題。ValueError當我在Python中運行文件

txt = input("Which file do you want to use?") 
file = codecs.open(txt,"r","utf-8") 


player=[] 
name=None 
longest_cast=0 
result=[] 

for line in file: 
row=line.split() 
row_1=len(row[1]) 
row_2=len(row[2]) 

if len(row)<5: 
    mu=float(row[1]) 
    sigma=float(row[2]) 


    for r in range(0,6): 
     cast=random.normalvariate(mu,sigma) 
     result.append(cast) 

if max(result)>longest_cast: 
      longest_cast=max(result) 
      name=row[0] 

print("The winner is", name, "who got", longest_cast, "meters. Congrats! :)") 

我得到這個錯誤代碼。

ValueError: max() arg is an empty sequence 

我該怎麼辦?有什麼建議麼?

+0

看起來結果等於[]和max([])拋出ValueError異常 –

+1

你真的需要在這裏修正你的縮進,python是空白敏感的,所以這裏的縮進可能會導致你的問題。 – shuttle87

+0

如果您的問題已解決,請刪除您的問題(如有可能),或接受(或發佈)答案。刪除您的問題內容對任何人都沒有幫助。 – deceze

回答

0

這裏的部分問題可能與縮進相關,因爲python是空白敏感的。在max被稱爲一個空序列的情況下,你會得到一個ValueError

>>> max([]) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
ValueError: max() arg is an empty sequence 
>>> max('') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
ValueError: max() arg is an empty sequence 

您需要檢查result實際包含的元素首先。

0

一些在數據文件中的行至少有五個項目,所以沒有下你if聲明縮進的東西永遠運行,包括for循環,追加到result列表。因此max()的參數是一個空列表,你會得到你指出的錯誤。

(有代碼中的其他縮進錯誤張貼,包括一個將保持它運行在所有。)