Theoretical question about IF

Hello everyone,

I am having a theoretical issue in Python. I don’t get the second IF in the code below. To me, a single number can be both <= 5 and <9. For example the number 4. Does Python treat it differently? Meaning, does the code implies that the second IF is <9 but also >5?

def movie_review(rating):
if(rating <= 5):
return “Avoid at all costs!”
if(rating < 9):
return “This one was fun.”
return "Outstanding!"This text will be hidden

no. But when rating = 4 (for example), the first if condition is true, which means we reach a return keyword. a return keyword means that the function hands back data to the caller and is done executing.

so the second if statement is never reached

if you hadn’t used return (but print instead), then your second if statement should have been elif.

ideally, you still should use else if, makes it easier to read and understand your code

2 Likes

Thanks for the help!

Got it. Are you then suggesting code should be written like this

def movie_review(rating):
if(rating <= 5):
return “Avoid at all costs!”
elif (rating < 9):
return “This one was fun.”
else: "Outstanding!"This text will be hidden

2 Likes