Python 3, Passing data from one function to another

Question:

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?

Asked By: user2832472

||

Answers:

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
Answered By: wvdz

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)
Answered By: jramirez

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()
Answered By: Ashwini Chaudhary
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

Answered By: Sakthi Geek
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.