1

I am kinda new to Python, and I would like to ask a question :

def spam(a):
    a = 1 + a
    return a
spam(21)
print(spam)
input()

After running it, the output is function spam at 0x021B24F8. Shouldn't the output be 22? Any help will be appreciated.

1 Answer 1

4

The problem is that your function, i.e. spam is returning a value. You need to accept the value returned by the function and store it in a different variable as in

s = spam(21)
print(s)

Here, you will store the returning value in the variable s which you will print it out.

After making the correction, the program will print as expected as in

22

Note - As mentioned, having a single statement print(spam(21)) also works as spam(21) will return 22 to the print function which will then print out the value for you!

Sign up to request clarification or add additional context in comments.

2 Comments

Or simply print(spam(21)).
@TimPietzcker Thanks, Have edited the answer to include your suggestion

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.