0
def main():
    uInput()
    calc()
def uInput():
    value1 = int(input('Please put in the first number'))
    value2 = int(input('Please put in your second number'))
    return value1
    return value2
def calc(value1,value2):    
    finalNumber = value1 + value2
    print (finalNumber)
main()

I am messing around with python, and am trying to make a simple calculator program. I'm trying to pass the input values from the uInput module, to the calc module. It keeps saying missing two required position arguments. Can you only pass one variable from a module to another module?

1
  • As a side note, uInput and calc are functions. Modules are a different thing, and when you get farther into the language and start importing things, you will get confused if you mix them up. Commented Nov 13, 2013 at 19:40

4 Answers 4

6

A function exits at the first return statement it encounters, so return value2 is never reached. To return multiple values use a tuple:

return value1, value2     #returns a tuple

Assign the returned value from uInput() to variables inside main:

val1, val2 = uInput()  #Assign using sequence unpacking 

Pass these variables to calc:

calc(val1, val2)       

Corrected version:

def main():
    val1, val2 = uInput()
    calc(val1, val2)

def uInput():
    value1 = int(input('Please put in the first number'))
    value2 = int(input('Please put in your second number'))
    return value1, value2

def calc(value1,value2):    
    finalNumber = value1 + value2
    print (finalNumber)
main()
Sign up to request clarification or add additional context in comments.

Comments

1

You can return two things at once from any function, but you can only use one return statement. by using return x, y this returns a tuple (x, y), which you can use in your main function.

def uInput():
    value1 = int(input('Please put in the first number'))
    value2 = int(input('Please put in your second number'))
    return value1, value2 # this returns a tuple

def main():
    val1, val2 = uInput() # unpack the tuple values into two variables
    calc(val1, val2)

2 Comments

Is it possible to return three values?
yes and the same principle applies. you will need val1, val2, val3 =
1

Basically, a function can only return once. When you use the return statement, the flow literally "returns", so the second return in your code is unreachable.

However, in python, you can return multiple values as a "tuple":

return value1,value2

Comments

0
def uInput():
    value1 = int(input('Please put in the first number'))
    value2 = int(input('Please put in your second number'))
    return value1, value2             # this returns a tuple

def calc(value1,value2):    
    finalNumber = value1 + value2
    print (finalNumber)

def main():
    val1, val2 = uInput()   # unpack the tuple values into two variables
    calc(val1, val2)     # this is one method which uses two temporary variables

    # ALternatively you can use python's argument unpacker(*) to unpack the tuple in the function call itself without using any temporary variables.

    calc(*uInput())

For more details check out http://docs.python.org/tutorial/controlflow.html#unpacking-argument-lists

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.