My function returns None but I print the result inside

AmeliaRunsOP
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'
2

Comments 5

Join the discussion

Already have an account? Log in

  1. ElaKowalskareplies: 2

    print shows a value on the screen, return gives it back to the caller. Your function prints but returns nothing, so t is None. Use return:

    def burn_time(fuel, rate):
        return fuel / rate
    4
    1. AmeliaRunsOPreplies: 1

      I thought printing was the same as returning. It's not!

      1
      1. JackStack

        Tip: let functions return values and do the printing outside. Easier to test and reuse.

        1
    2. TomBecker

      Very common confusion. The challenge "Prints instead of returns" is built around it.

      3
      1. OllieGrant

        A function without return (or with plain return) always returns None. Good to know when debugging.

        2