Help me with this Function bug (I am an early beginner)

``````def inv():
print("Please enter a numerical value between the range of  1.00 - 0.0")
if float(marks) >= 0.9:
elif float(marks) >= 0.8:
elif float(marks) >= 0.7:
elif float(marks) >= 0.6:
elif float(marks) < 0.6:
return x
while True :
try :
score = float(input("Enter your mark: "))
if score > 1.00 or score < 0.00:
raise
else :
break
except :
inv()
continue
``````

The output is :

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.

Where are you printing thr grade. In the main program or in ```
compute_grade? The mistake repeats its self 5 times.
John

Sorry, i edited the post please refresh. The past post was just a glitch in the code. I reran the code and now its a diff error ;I

âItâs a diff errorâ? What is the error?

`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:`.

1 Like

OO THANKS A LOT, I genuinely appreciate your explanation. Helps a lot so with the raise one i shouldâve did

``````while True :
try :
score = float(input("Enter your mark: "))
if score > 1.00 or score < 0.00:
raise ValueError
else :
break
except ValueError :
inv()
continue
``````

it works now thank you

I would suggest you do not use the `elif float(marks) < 0.6:` just use `else:`

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

Can you share your current code with the else change so we can comment on the error you are seeing?

The final condition is guaranteed to be true, so why have it?

`if` and `elif` require conditions, but `else` doesnât.