def inv():
print("Please enter a numerical value between the range of 1.00 - 0.0")
def compute_grade(marks):
if float(marks) >= 0.9:
x = print("Grade A")
elif float(marks) >= 0.8:
x = print("Grade B")
elif float(marks) >= 0.7:
x = print("Grade C")
elif float(marks) >= 0.6:
x = print("Grade D")
elif float(marks) < 0.6:
x = print("Grade F")
return x
while True :
try :
score = float(input("Enter your mark: "))
if score > 1.00 or score < 0.00:
raise
else :
print(compute_grade(score))
break
except :
inv()
continue
The output is :
Grade is (the grade obviously)
Please Enter the numerical⌠etc.
Why is the raise occur??? I added a break after the print and the loop starts again⌠when i enter the wrong input obv the INV() activated which is nice.
compute_grade is printing the grade and then assigning the result of print to x. print always returns None, so itâll always assign None to x. Itâs then returning x, i.e. None, which is printed in the main loop.
Either print in compute_grade or return the grade from compute_grade and then print in the main loop, not both. The latter option is probably better.
Using raise on its own is for when youâre re-raising an exception that youâve caught. If youâre raising an exception the first time, you must specify the exception that youâre raising, e.g. raise ValueError('invalid mark').
Using a âbareâ exception, i.e. except:, is a VERY bad idea because it catches ANY error. It even catches the exception generated by the incorrect use of raise! Always specify the exception that you want to catch and handle, e.g. except ValueError:.
Cool, any reasons why?
And I want to ask a question, if I change elif to âelseâ but keep the float(marks) <0.6:⌠the parameter will be invalid, why?
Thanks for the help otherwise though