嗯,这是一个家庭作业项目,我有点难过。我试过做str / int转换。原来是行allScores.append ( int( strScore / 10 )
- 但我发现/ 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和基本编程。
任何有用的建议?
答案 0 :(得分:0)
暂时离开int()
演员表。将所有内容作为字符串附加,然后,当您完成循环时,pop()
最后一个元素('done'
)并将剩余的allScores
转换为整数(使用{ {1}},理解,等等。
map()