0
def x1():
    y = input("Input pet here: ")
    if y == "pet":
        return True
    else:
        return False
x1()

def x2():
    y = input("Input pet here: ")
    if y == "pet":
        print(y)
    else:
        print("not a pet")
x2()

Output: C:\Users\jiraf\AppData\Local\Programs\Python\Python39\python.exe "C:/Users/jiraf/OneDrive/Documents/Grzegorz/Programowanie/Python/kurs/1.02/Wykład 1.8 Funkcje (definiowanie, argumenty).py"

Input pet here: shit Input pet here: shit not a pet

Process finished with exit code 0 enter image description here

I have tried with many easy functions that simply should return something

I have no idea why is that.

Jirafey
  • 57
  • 8

2 Answers2

1

This is definitely not a interpreter issue. I can't quite tell what editor you are running, but I'm pretty certain that editors such as Jupyter only print the last executed line in a chunk. That is very likely what is going on here. You should either run print(x1()) or something like

x = x1()
y= x2()
print(x)
print(y)
bdempe
  • 308
  • 2
  • 9
  • it's Pycharm, it's just my bad I thought it would just print value automatically. – Jirafey Feb 01 '22 at 18:05
  • 1
    Totally understandable. Its a strange nuance to get tripped up on if you don't expect it. More importantly, though, is the difference between printing a value to the console and returning a value. Both method calls return a value, but only one is printed to console. – bdempe Feb 01 '22 at 18:15
1

return does not print to output, it just returns function result. You may have seen this printing when using python shell as it does print result for some needed reasons.

For your 1st function to print you must print the called function like this

print(x1())
sudden_appearance
  • 1,968
  • 1
  • 4
  • 15
  • what do you mean by python shell? – Jirafey Feb 01 '22 at 18:04
  • 1
    As a tip. Python fucntions always return (except maybe some built-in functions). Either you declare return statement explicitly, or python function returns `None` when it reaches the end of function. The function is always a thing that returns. Function is also callable, so the print(x1()) actualy does next steps. Python sees nested calls, so it calls from inside. First it calls x1, which returns True or False, after the result is returned, print is called with that result, so you end up with either print(True) or print(False) – sudden_appearance Feb 01 '22 at 18:08
  • 1
    @Jirafey python shell is a tool that comes with python. When you want to run your commands line by line or some other reasons you can just type `python` or `py` or `python3` in console or terminal and by doing that you will start python shell – sudden_appearance Feb 01 '22 at 18:11