Why does my function print "none"?

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”

5 Likes

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!”

2 Likes

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

2 Likes

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.

5 Likes

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.
2 Likes

Hi, When I was writing below solution with “print” keyword, I was getting None when I was running code. But when I replaced “print” keyword with “return”, it stopped giving None in the console.

“”“A school has following rules for grading system:
a. Below 25 - F
b. 25 to 45 - E
c. 45 to 50 - D
d. 50 to 60 - C
e. 60 to 80 - B
f. Above 80 - A
Ask user to enter marks and print the corresponding grade.
“””

def count_grade():
grade = int(input("Enter your marks: "))
if grade < 25:
return “Grade: F”
elif grade > 25 and grade < 45:
return “Grade: E”
elif grade > 45 and grade < 50:
return “Grade: D”
elif grade > 50 and grade < 60:
return “Grade: C”
elif grade > 60 and grade < 80:
return “Grade: B”
elif grade > 80:
return “Grade: A”

print(count_grade())

Functions in Python always return a value even when no return statement is executed. The default is to return the None object- https://docs.python.org/3/tutorial/controlflow.html#defining-functions

You may not notice this unless you print the output of your function but it is worth being aware of (e.g. the None is actually falsey: bool(None) is False).