FAQ: Code Challenge: Control Flow - Movie Review

This community-built FAQ covers the “Movie Review” exercise from the lesson “Code Challenge: Control Flow”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Movie Review

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi guys, I’m just getting familiar with the basics of Python. However I don’t understand, why I get here the print out on terminal “None” 3x times. See picture. Thanks for help.code_revi

I am also new, I think, the issue could be that you used print for every if and else. I think, when you use return then it will store the value, without return it will be “None”

Here is mine:
def movie_review(rating):
if rating <= 5:
return “Avoid at all costs!”
elif rating > 5 and rating < 9:
return “This one was fun.”
else:
return “Outstanding!”

1 Like

@microrunner74470 Hi, did you understand his answer? I’d like to explain it to you further if not.

so awesome thing when testing ranges that i found,

if 5 <= rating < 9:
    return "This one was fun."

Was over here at stackoverflow

Handy when all we want to check is one interval. If there are multiple intervals then it would overkill when simple comparisons will do the job.

You hit on ‘range’, and that’s where an inequality expression is handy.

def double_at_index_if(array, index):
    n = len(array)
    if -n <= index < n:
        array.insert(index, array.pop(index) * 2)
    return array

Above may look similar to an exercise we did a while back, only switched it for a bit of fun. The array will be manipulated at any valid index.

It gets real fun when we introduce exception handling.

def double_at_index_try(array, index):
    try:
        array.insert(index, array.pop(index) * 2)
        return array
    except (IndexError, ValueError, TypeError):
        return array
print (double_at_index_if([2, 5, 7, 9], 2))
print (double_at_index_try([2, 5, 7, 9], 2))
print (double_at_index_if([2, 5, 7, 9], -5))
print (double_at_index_try([2, 5, 7, 9], 4))
[2, 5, 14, 9]
[2, 5, 14, 9]
[2, 5, 7, 9]
[2, 5, 7, 9]

Hello @cryptopotus963635118 Could you please explain the function of else statement further?

In conjunction with the

  1. Movie Review exercise —
    def movie_review(rating):
    if(rating <= 5):
    return “Avoid at all costs!”
    elif(5 <= rating < 9):
    return “This one was fun.”
    return “Outstanding!”

print(movie_review(9))

should print “Outstanding!”

print(movie_review(4))

should print “Avoid at all costs!”

print(movie_review(6))

should print “This one was fun.”

as well as
2. Twice as Large exercise Twice as Large

I am confused as to why does the else statement not work in the Movie Review exercise, but works in the Twice as Large one.

I don’t see any ‘else’ statement in your example. The way this function runs depends upon the indentation, which we can only guess at. To properly enter code, use the </> icon found in the menu bar at the top of the text box when you are typing. Did you mean this?

def movie_review(rating):
    if(rating <= 5):
        return 'Avoid at all costs!'
    elif(5 <= rating < 9):
        return 'This one was fun.'
    return 'Outstanding!'
print(movie_review(9))
print(movie_review(4))
print(movie_review(6))

# Output:
Outstanding!
Avoid at all costs!
This one was fun.

I think that you might be asking why else was omitted here. Is that right?

else could be put in, so that the function ended with

  else:
    return 'Outstanding'

… and it would work the same way, but it is unnecessary (as it is in the twice_as_large() example.) The key is the indentation of the final return statement. Remember that return returns a value and then halts the function.

So in the “movies” example,

  • If the rating is less than or equal to 5, the first if block is entered, function returns ‘Avoid at all costs!’, and then stops.

  • If the rating is greater than 5, but less than 9, the first if block is bypassed, the elif block is entered, , the function returns ‘This one was fun’, and then stops.

  • If the rating is greater than 9, both the if and the elif are skipped. Now, at this point, we don’t care what the rating is. We know that it is greater than 9, so no more categories are needed. The only thing left to do is return ‘Outstanding’ and halt the function. That would happen whether return were inside of an else block, or placed at the left-most indentation level within the function, as shown above.

1 Like

I had a problem on this particular lesson where I wrote the correct code, like so:

def movie_review(rating):
  if rating <= 5:
    return "Avoid at all costs!"
  if rating > 5 and rating < 9:
    return "This one was fun."
  else:
  	return "Outstanding!"

and yet a SyntaxError message appeared suggesting that there was something wrong with the less than arrowhead before 9 on line 4. Now I had no idea what I did wrong, but I was for sure right for the most part. I am just curious if it was me who got the code wrong or Codecademy

Inconsistent indentation could be the problem.

Aside

Python lets us write inequalities…

if 5 < rating < 9:

which gives matched relational operators.

Since you are using return in each if statement, no else is required.

if ...:
    return ...
if ...:
    return ...
return "Outstanding!"

Wondering if my second condition would be correct? if(rating < 9): numbers 1-5 should only return "Avoid at all costs!``` then it would stop reading the code and not return “Avoid at all costs!”. thanks

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

When the action of a conditional branch is to return then it will have already been excluded if the next conditional is reached. Yes, at this point (the second if statement) the value is greater than 5 so your thinking is correct.

Bottom line, return is final. Nothing is reachable after that.

1 Like

I’m sure you figured this out by now but for posterity this is because when a function is called, by default if there is no return value, the value is set to none.

Your function is printing based off the condition of the if statement. The function call "print(movie_review(9)) is attempting to print the return value. Without a return statement this defaults to none.

It can be fixed by adding a return statement on each conditional statement and removing the print.

For example:

if rating <=5:
return “Avoid at all costs!”

OR

You can change the function call to remove print, and keep your code the way it is.

For example:

movie_review(9)

Where we have removed print from the call. You’d have to do this for all function calls.

1 Like

Unless otherwise specified in the assignment (instructors and bosses have the final word on this), I think that the “best practice” is to make almost exclusive use of return.

If you want to see your output, make use of the function call.

def odd_or_even(x):
    '''Input int x
        return whether x is odd or even
    '''
    if x % 2 == 0:
        return "Even"
    else:
        return "Odd"
    
print(odd_or_even(3))
''' Output:
Odd
'''

Note that In accordance with PEP-8, if you return at the end of a conditional block, you should use return at the and of all.

Exceptions:

  1. Debugging: the print() function is terrific for debugging. Make use of the “comma-separated” (tuple parameter) capability, which will print your data without converting to str, and you can keep track of all your variables. Add labels. If there are lengthy lists involved, print only the first and last few elements. Ok, I know that most IDE’s have built-in debuggers to show variables, but I love print(), so there it is. (You will need to comment out a lot of print statements before submitting an assignment, though!)

  2. Printing. Oh, yes, your program may actually need to print stuff. Code one function whose job it is to do all the nice formatting, and have the others send their data to it.

Remember:

  • Use print() to put data on the screen that you want to see
  • Use return to pass data the Python needs to use: to assign to a variable, pass to a function, or serve as an operand in a calculation.