My function returns None but I print the result inside
def burn_time(fuel, rate):
print(fuel / rate)
t = burn_time(100, 4)
print(t * 2) # TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'def burn_time(fuel, rate):
print(fuel / rate)
t = burn_time(100, 4)
print(t * 2) # TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'
Comments 5
Join the discussion CommentAlready have an account? Log in
printshows a value on the screen,returngives it back to the caller. Your function prints but returns nothing, sotis None. Use return:I thought printing was the same as returning. It's not!
Tip: let functions return values and do the printing outside. Easier to test and reuse.
Very common confusion. The challenge "Prints instead of returns" is built around it.
A function without return (or with plain
return) always returns None. Good to know when debugging.