{"id":"return-values-return-or-print","slug":"return-or-print","title":"Return or print?","text":"A return value is a value like any other. You can print it, store it, calculate with it, compare it or give it to another function as an argument:\n\n```python\ndef to_minutes(hours, minutes):\n    return hours * 60 + minutes\n\n\ndef to_hours_text(minutes):\n    return str(minutes // 60) + \" h \" + str(minutes % 60) + \" min\"\n\n\nfilm = to_minutes(1, 50)\nseries = 3 * to_minutes(0, 45)\nprint(film, series)\nif film < series:\n    print(\"The film is shorter\")\nprint(to_hours_text(film + series))\n```\n\n```output\n110 135\nThe film is shorter\n4 h 5 min\n```\n\nIn the last line, `film + series` is worked out first, its value 245 is given to `to_hours_text`, and the string it returns is given to `print`. Calls inside calls run from the inside out.\n\n### Return and print are different\n\nA function that prints its result and a function that returns it can look alike when you try them out. Compare these two in the interpreter:\n\n```pycon\n>>> def longer_print(a, b):\n...     if len(a) >= len(b):\n...         print(a)\n...     else:\n...         print(b)\n...\n>>> def longer(a, b):\n...     if len(a) >= len(b):\n...         return a\n...     return b\n...\n>>> longer_print(\"cat\", \"horse\")\nhorse\n>>> longer(\"cat\", \"horse\")\n'horse'\n>>> longer(\"cat\", \"horse\").upper()\n'HORSE'\n>>> result = longer_print(\"cat\", \"horse\")\nhorse\n>>> print(result)\nNone\n```\n\nThe interpreter shows the difference: `horse` without quotes is text that the function printed, while `'horse'` with quotes is a value that it returned. You can go on working with the returned value, like calling `upper` on it. The printed text is only on the screen: the code that called `longer_print` gets nothing back.\n\nA function that ends without a `return` gives back the special value **`None`**, which means \"no value\". Using `None` as if it were a string or a number stops the program:\n\n```python\ndef longer_print(a, b):\n    if len(a) >= len(b):\n        print(a)\n    else:\n        print(b)\n\n\nword = longer_print(\"cat\", \"horse\")\nprint(len(word))\n```\n\n```output\nhorse\n```\n\n```crash\nTraceback (most recent call last):\n  File \"main.py\", line 9, in <module>\n    print(len(word))\n          ~~~^^^^^^\nTypeError: object of type 'NoneType' has no len()\n```\n\n`NoneType` is the type of `None`. When you see it in an error message, a function probably printed something instead of returning it, or forgot its `return`.\n\nReturning is usually the better choice, because the result can still be used in any way, printing included. That's why the checks of a task that asks for a return value fail when your function prints the right text instead of returning it.\n","task":"`average(a, b, c)` prints the average of its three arguments, but it should return it. Change it so it returns the average and prints nothing. For example, `average(2, 4, 9)` returns `5.0`.\n","kind":"code","seed":"def average(a, b, c):\n    print((a + b + c) / 3)\n\n\nif __name__ == \"__main__\":\n    average(2, 4, 9)\n","solution":"def average(a, b, c):\n    return (a + b + c) / 3\n\n\nif __name__ == \"__main__\":\n    print(average(2, 4, 9))\n","hints":["Replace the `print(...)` with a `return` statement that gives back the same value.","Use `return` with the same calculation that `print` has now. To still see the result when you run the program, print the call in the main block: `print(average(2, 4, 9))`."],"tests":[{"name":"average(2, 4, 9)","stdin":"","call":"average(2, 4, 9)\n","expect":"5.0","hidden":false,"returns":true},{"name":"average(1, 1, 2)","stdin":"","call":"average(1, 1, 2)\n","expect":"1.3333333333333333","hidden":true,"returns":true},{"name":"average(10, 20, 30)","stdin":"","call":"average(10, 20, 30)\n","expect":"20.0","hidden":true,"returns":true}],"require":[],"forbid":[],"more":[{"task":"The function `minutes(hours, mins)` in the starter code turns a time of day into minutes since midnight. Use it to write the function `duration(start_h, start_m, end_h, end_m)`, which returns how many minutes there are from the start time to the end time on the same day. For example, `duration(9, 15, 10, 45)` returns `90`.\n","kind":"code","seed":"def minutes(hours, mins):\n    return hours * 60 + mins\n\n\ndef duration(start_h, start_m, end_h, end_m):\n    pass\n\n\nif __name__ == \"__main__\":\n    print(duration(9, 15, 10, 45))\n","solution":"def minutes(hours, mins):\n    return hours * 60 + mins\n\n\ndef duration(start_h, start_m, end_h, end_m):\n    return minutes(end_h, end_m) - minutes(start_h, start_m)\n\n\nif __name__ == \"__main__\":\n    print(duration(9, 15, 10, 45))\n","hints":["Turn both times into minutes since midnight with `minutes`: `minutes(start_h, start_m)` and `minutes(end_h, end_m)`.","The duration is the end in minutes minus the start in minutes.","Check your function with `duration(12, 50, 13, 5)`: 13:05 is 785 minutes after midnight and 12:50 is 770, so it should return 15."],"tests":[{"name":"duration(9, 15, 10, 45)","stdin":"","call":"duration(9, 15, 10, 45)\n","expect":"90","hidden":false,"returns":true},{"name":"duration(8, 0, 16, 30)","stdin":"","call":"duration(8, 0, 16, 30)\n","expect":"510","hidden":true,"returns":true},{"name":"duration(12, 50, 13, 5)","stdin":"","call":"duration(12, 50, 13, 5)\n","expect":"15","hidden":true,"returns":true}],"require":[{"pattern":"\\bminutes\\s*\\(","min":3,"message":"Call `minutes` for both times instead of calculating them again."}],"forbid":[]},{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"def shout(word):\n    print(word.upper())\n\n\ndef whisper(word):\n    return word.lower()\n\n\na = shout(\"hey\")\nb = whisper(\"HEY\")\nprint(a)\nprint(b)\nprint(whisper(\"OK\") + \"!\")\n","solution":"def shout(word):\n    print(word.upper())\n\n\ndef whisper(word):\n    return word.lower()\n\n\na = shout(\"hey\")\nb = whisper(\"HEY\")\nprint(a)\nprint(b)\nprint(whisper(\"OK\") + \"!\")\n","hints":["`shout` prints its word in capitals when it's called, and returns nothing.","A function without `return` gives back `None`, so `a` is `None`.","`whisper` prints nothing itself: its values are printed by the `print` lines at the end."],"tests":[{"name":"Output","stdin":"","expect":"HEY\nNone\nhey\nok!","hidden":false}],"require":[],"forbid":[],"lines":["HEY","None","hey","ok!"]}]}