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")
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:
          else :
     except :

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.

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

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 :slight_smile: 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 :
     except ValueError :

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.