+ 1
I wrote a simple code by using function definition that computes grades. Why do I get Traceback error for non integers inputs?
5 Answers
+ 21
Whithawk u have declared user_input in while loop
so it is not accessible outside while loop....
try this đ
https://code.sololearn.com/c6bagyJ0u9wt/?ref=app
+ 1
you try to convert your input to an int... so 45.5 raises an error...
+ 1
Thanks alot đDTđ. I stil have a lot to learn. I just started learning python though.
0
Amaras A thank you. However it stil gives a Traceback error for input that are not numbers.. Like letters.
0
I think I fixed it.
def computegrades(x):
if x >= 70:
print("A")
elif x >=60:
print("B")
elif x >=50:
print("C")
elif x >=45:
print("D")
elif x <45:
print("F")
else:
print("invalid score. Try again")
return
user_input = input("Type your score to compute grade: ")
if user_input.isdigit():
x = int(user_input)
computegrades(x)
else:
print("That is not a valid number. Try again!")