Reputation: 3
I have to debug this program, and for the life of me, I can't figure it out. I don't understand why the while loop just isn't running, and when I enter 'done' I just get a value error.
# Constant Definitions
MAX_SCORE = 10
MIN_SCORE = 0
def GetValidInput ( prompt ):
"""
This function continues to ask the user for valid input until it
it received. The acceptable inputs are integers between 0 and 10
(inclusive) and the string "done"
Preconditions: Prompt is expected to be a string that is printed
out each time the user is asked for a new input.
Postconditions: The valid input is returned as a string.
"""
strScore = input ( prompt )
while ( not str.isdigit(strScore) and strScore != "done" ) or \
( str.isdigit(strScore) and \
( int(strScore) < MIN_SCORE and int(strScore) > MAX_SCORE ) ):
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
# Program Instructions
print ( "Enter the homework scores one at a time. Type 'done' when finished." )
allScores = [ 1 ]
strScore = GetValidInput ( "Enter HW#" + str(len( allScores )) + " score: " )
while ( strScore != "done" ):
allScores.append ( int(strScore) / MAX_SCORE )
strScore = GetValidInput ( "Enter HW#" + str(len( allScores )) + " score: " )
letterGrade = "I"
if ( len( allScores ) >= 1 ):
pctScore = sum ( allScores ) // ( len ( allScores ) * 100 )
elif ( len( allScores ) < 1 ):
pctScore = 0
elif ( pctScore < 60 ):
letterGrade = "F"
elif ( pctScore < 70 ):
letterGrade = "D"
elif ( pctScore < 77 ):
letterGrade = "C"
elif ( pctScore < 80 ):
letterGrade = "C+"
elif ( pctScore < 87 ):
letterGrade = "B"
elif ( pctScore < 90 ):
letterGrade = "B+"
else:
letterGrade = "A"
print ( "Your percentage score is %.2f%% and your letter grade for the course is a %0s." \
% ( pctScore, letterGrade ) )
#end
At first I didn't have isdigit called correctly. I thought I fixed it but i guess its still wrong because its still not working. I have no idea what else to try, I'm really stuck
Upvotes: 0
Views: 119
Reputation: 3130
At a rough guess, the main issue is that the script is written to run under python 3, see the print statements, and you're running it under python 2.
The first fix you should try is to find the python 3 install, and use that. If you cannot do that, change the 'input' call to 'raw_input', which should fix the value errr issue.
Upvotes: 0
Reputation: 122116
For one thing, you calculate percentages incorrectly:
pctScore = sum ( allScores ) // ( len ( allScores ) * 100 )
Should be
pctScore = (sum(allScores) // len(allScores)) * 100
Secondly, this line:
elif ( pctScore < 60 ):
Should just be if
, otherwise it only runs if len(allScores)
is neither less than nor greater than or equal to one, i.e. never.
Finally, please review PEP-008 and format your code sensibly.
Upvotes: 0
Reputation: 599876
You might like to ponder under what conditions this condition could ever be true:
int(strScore) < MIN_SCORE and int(strScore) > MAX_SCORE
Upvotes: 3