Well, this is kind of a homework project, and I am sort of stumped. I have tried to do str/int conversions. The original was line was allScores.append ( int( strScore / 10 )
- But I found the /10 to be redundant, as I wanted to append the value itself. I am debugging a program deliberately full of flaws for simple grade calculations.
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))
For some reason, I keep gettingValueError: invalid literal for int() with base 10: 'done'
, but I definitely need to append the value to the list, and get an average. Is there a way to do this without adding in another IF? Still learning python and basic programming here.
Any helpful advice?
Just leave the int()
casting for later. Append everything as a string, and then, when you're done with the loop, pop()
the last element (the 'done'
) and convert the remaining allScores
to integers (with map()
, a comprehension, whatever).
while strScore != "done":
strScore = GetValidInput("Enter HW#{} score: ".format(len(allScores)))
allScores.append(strScore)
print(allScores)
allScores.pop()
allScores = list(map(int, allScores))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.