Home > front end >  Return keyword doesn't return
Return keyword doesn't return

Time:08-04

The return keyword is supposed to return a value back to the user and then exit the function. I understand the part where the function is being exited, however, not the return part itself. For example, I have this simple code here:

def my():
    x = "something"
    return x
my()

Why doesn't the return keyword return any value, in this case, x which is "something"

CodePudding user response:

Having my() simply does nothing as you are just running the function, you have to assign it to a variable:

output = my()

or print it directly:

print(my())

CodePudding user response:

You need to handle data which function my() is returning. You need to assign it to variable or print it (depends what you want to do it in future). For example:

def my():
    x = "something"
    return x
my_variable = my()
print(my_variable)

CodePudding user response:

I recommend reading to better understand https://appdividend.com/2022/01/19/python-return/

  • Related