I usually run Python on Google Colab, however I need to run a script in the terminal in Ubuntu.
I have the following script
test.py:
#!/usr/bin/env python
# testing a func
def hello(x):
if x > 5:
return "good"
else:
return "bad"
hello(2)
When executed it fails to return anything. Now I could just replace the return statements with a print statement. However, for other scripts I have, a return statement is needed.
I tried:
python test.py
You see, on Google Colab, I can simply call the function (hello(2)) and it will execute.
Desired output:
> python test.py
> bad
2
Answers
You don’t print anything to
STDOUT
so you won’t see thegood
/bad
in your terminal.You should change
hello(2)
line toprint(hello(2))
in your code (In this case the return value ofhello(2)
function call will be printed toSTDOUT
file descriptor) then you will see your result in your terminal.In case you want to sent the argument when calling the script you could do it like this:
And so you could call the function like so:
Then the output would be: