-1

I wrote a function that printed out the min & max of the numbers in the list, but when I switched print with return the code doesn't seem to print a thing, so I ran the same code with the return statement in the python console and it work, I want to know why the code works with the python console in terminal and why it doesn't when I run the script as python3 Code.py

#!/usr/bin/env python3.5
#
#
def Ehlo():
    nums = [1,5,2,4,6,12,8,9,3]

    return (min(nums))
    #return (max(nums))

Ehlo()
TeachAble
  • 17
  • 2
  • You should read more about how return works. This should be a good start: http://stackoverflow.com/questions/7129285/why-would-you-use-the-return-statement-in-python – Kevin K. Jul 27 '16 at 23:43
  • What did you expect the code to do by having two return statements ? maybe what you actually wanted is something like: `return min(nums), max(nums)` – Nir Alfasi Jul 27 '16 at 23:45
  • Also see [here](http://stackoverflow.com/questions/21278736/why-doesnt-python-print-return-values), [here](http://stackoverflow.com/questions/3881434/difference-between-returns-and-printing-in-python), and a ton of other results that you can find by Googling the difference between printing and returning. – TigerhawkT3 Jul 27 '16 at 23:58
  • For the same reason that putting `1 + 1` in a file by itself and running it doesn't display `2`. – Karl Knechtel Aug 12 '22 at 06:19

2 Answers2

2
  1. You cannot use 2 returns. The first return will return the minimum value and the second return will never be reached.
  2. The value returned is not being printed.

Try this -

def Ehlo():
   nums = [1,5,2,4,6,12,8,9,3]
   return (min(nums)),(max(nums))

print(Ehlo())

Output: (1,12)

donkopotamus
  • 22,114
  • 2
  • 48
  • 60
aramase
  • 114
  • 3
0

This is how Python behaves: In a script you will always need a print statement to see a visible result. However, in the console entering the name of a variable (or returning something as in your case from a routine) will automatically print the value.

dkar
  • 2,113
  • 19
  • 29