嗯,這是一種家庭作業項目,我有點難住。我試圖做str/int轉換。原來的行是allScores.append (int(strScore/10)
- 但我發現/ 10是多餘的,因爲我想附加值本身。我正在調試一個故意充滿簡單等級計算缺陷的程序。ValueError:int()與基數10的無效文字:'完成'
MAX_SCORE = 10
MIN_SCORE = 0
def GetValidInput (prompt):
strScore = input (prompt) # Added() to isdigit
while (not strScore.isdigit() and strScore != "done") or \
(strScore.isdigit() and \
(int(strScore) < MIN_SCORE or int(strScore) > MAX_SCORE)): #Added int() as is it was comparing strings and numbers -- Changed AND to OR
if (strScore.isdigit()):
print ("Please enter a number between %0d and %0d." \
% (MIN_SCORE, MAX_SCORE), end=' ')
else:
print ("Please enter only whole numbers.", end=' ')
strScore = input (prompt)
return strScore
allScores = [ ]
strScore = GetValidInput ("Enter HW#" + str(len(allScores)) + " score: ") #Added str(), as is printing a string
while (strScore != "done"):
strScore = GetValidInput ("Enter HW#" + str(len(allScores)) + " score: ") # Fixed GetvalidInput to GetValidInput -- Added str() to allScores to work in string -- Changed line place with the one below
allScores.append (int(strScore)) # Fixed MAXSCORE to MAX_SCORE -- Added int() as it was comparing string strScore to an Integer -- Changed line place with the one above -- Removed
print(str(allScores))
出於某種原因,我不斷收到
ValueError: invalid literal for int() with base 10: 'done'
,但我一定要值追加到列表中,並得到一個平均值。有沒有辦法做到這一點,而不添加另一個IF?在這裏仍然學習python和基本的編程。
任何有幫助的建議嗎?
您的'GetValidInput()'函數保證返回''done''。然後你嘗試將其轉換爲'int()'。 – TigerhawkT3
我需要一個整數列表,但我需要完成循環。你會建議什麼?我查看了你說的話並刪除了int(),但後來似乎遇到了一個總結列表中數字的問題。 除非我可以使它工作 pctScore = sum(allScores)//(len(allScores)* 100) – Adrian