"else" statement not necessary?

https://www.codecademy.com/courses/learn-python-3/lessons/python-functions-control-flow-cc/exercises/movie-review

Hi, I’m curious as to how come I don’t have to include “else:” in this code and it works just the same as if I did.:

1 Like

Hi @cryptopotus963635118,

When a return statement is executed, execution of the function that contains it is terminated and a value is returned. No additional statements within the function are executed at that time unless the function had called itself.

For the movie_review function, let’s consider this call:

print(movie_review(9))

The rating parameter has the value 9. The tests in the first if header and then the second evaluate to False, so neither of the return statements within the associated if blocks is executed. Control proceeds to the third return statement and it is executed.

If the rating parameter had a value less than 9, one of the other two if blocks would execute. For example, if it were 3, the first one would execute. If it were 7, the first one would not execute, but the second one would do so.

If you want, you could achieve the same effect with if, elif, and else blocks.

Edited on March 3, 2019 to provide examples of what would occur with various values for rating.