Besides the error with the (
going through the code some modifications could make this a little more optimal. We can use a try, except
block to eliminate the ERROR
else statement and handle that directly when we receive the input
if is not a valid int
and we force a valid int
between 0 and 100
using a while statement. Next our function should return 'A'
or B, C
etc. Then when we can use the print('Your grade is: ', printgrade(score))
as desired
def printgrade(score):
if score >= 90:
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
elif score >= 65:
return "D"
else:
return "F"
def main():
score=""
while score not in range(0,101):
try:
score = int(input("Enter a score: "))
except ValueError:
print('Invalid Entry')
print("Your grade is:", printgrade(score))
main()
2
solved How To Determine Grades in Python [closed]