0

I just wrote this very simple Python script which finds the sum of two integers. I'm just trying to understand how the return values are assigned to variables:

def add(a,b):
    c = a + b
    return a
    return b
    return c

first_number, second_number, result = add(3,4)

print 'first_number is ', first_number
print 'second number is ', second_number
print 'result is ', result

When I try to run this script, I get the following:

Traceback (most recent call last):
  File "test.py", line 7, in <module>
    first_number, second_number, result = add(3,4)
TypeError: 'int' object is not iterable

Why is that? And, how can I solve this issue?

0

1 Answer 1

8

Your problem is that the return a statement stops the routine and returns only one value, but the main program expects three values. Your other return statements are never executed.

Instead, use just one return statement:

def add(a,b):
    c = a + b
    return a, b, c
Sign up to request clarification or add additional context in comments.

1 Comment

Indentations mean only when they come after an outer one. So when you want to use a line of code you should write it in correct style.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.