0

How do I pass multiple (2 or more) variables from a function to another in python? I have searched all around the internet, yet I am unable to understand most of the explanations given, many examples just do not fit my case, and I cannot fix the problem with a MRE. Here is the example code:

def prompt_user():
    money1 = input("insert a value")
    money2 = input(" insert another value")
    return money1, money2

def add(money1, money2):
    cash = int(money1) + int(money2)
    return cash

two_values = prompt_user()
total = add(two_values)
print("you have "+ str(total))

Is this just unable to be done in python? Or do you have to use something like lists to pass arguments in this case?

Examples I have found(yet not understood): Python — Passing Multiple Arguments https://www.geeksforgeeks.org/how-to-pass-multiple-arguments-to-function/

EDIT: I fixed it. Turns out we have to break the tuple when passing values from another function. Thank you everyone.

  • 1
    If you have found explanations of how this works but fail to understand them, it would be helpful if you cited (some of) them. Otherwise, chances are we will just explain the same thing again. – MisterMiyagi Dec 03 '20 at 09:41
  • `two_values` is a `tuple`. Unpack it with an asterix. `total = add(*two_values)` – Tom Wojcik Dec 03 '20 at 09:41
  • Does this answer your question? [Passing multiple arguments to a function?](https://stackoverflow.com/questions/44329135/passing-multiple-arguments-to-a-function) – MisterMiyagi Dec 03 '20 at 09:42
  • `two_values` is a tuple. You can unpack it as `money1, money2 = prompt_user()` then call the method. `total = add(money1, money2)` – MjZac Dec 03 '20 at 09:43

4 Answers4

1

I think the problem is due to the definition of arguments in method definition. So in general you can pass lists, functions, etc. to a function. In this case, I have collected the inputs in a tuple and pass the tuple to the function.

def prompt_user():
    money1 = input("insert a value")
    money2 = input(" insert another value")
    z=(money1,money2)
    return z

def add(z):

    cash = int(z[0]) + int(z[1])
    return cash

two_values = prompt_user()
total = add(two_values)
print("you have "+ str(total))
Mattvers
  • 46
  • 7
0

You either want to unpack those values on function call:

total = add(*two_values)

Or unpack them on assignment:

val_one, val_two = prompt_user()
total = add(val_one, val_two)
go2nirvana
  • 1,598
  • 11
  • 20
0

If you run it like this it works:

money1 = 5
money2 = 7

def add(money1, money2):
    cash = int(money1) + int(money2)
return cash

total = add(money1,money2)
print("you have "+ str(total))

As you can see you just have to put the first variable , and then second variable.

If you have the prompt you can just split the answers as:

money1, money2 = prompt_user()

EDIT:

def prompt_user():
money1 = input("insert a value")
money2 = input(" insert another value")
return money1, money2

def add(money1, money2):
    cash = int(money1) + int(money2)
return cash

money1, money2 = prompt_user()
total = add(money1, money2)
print("you have "+ str(total))

I would change the last sentence to

print(f'You have {total}')
Hestaron
  • 190
  • 1
  • 8
  • Can I still do something similar with two different functions like in my example? – penjelmaan katak Dec 03 '20 at 09:46
  • I edited my post for clarification. Hope it works like this. The last sentence is an f-string. Google it, it makes printing strings with variables much easier. – Hestaron Dec 03 '20 at 16:13
0
def prompt_user():
    money1 = int(input("Insert a value: "))
    money2 = int(input("Insert another value: "))
    # You can get many more input like above
    
    return (money1, money2) # pass paratmeter inside () as tuple

def add(values):
    return sum(list(values))


multi_vaules = prompt_user()
total = add(multi_vaules)
print("you have "+ str(total))

If you need to get sum of any variable then you follow the code

def prompt_user():
    money1 = input("Insert a value: ")
    money2 = input("Insert another value: ")
    # You can get many more input like above
    
    return (money1, money2) # pass paratmeter inside () as tuple

def add(values):
    res=''
    for val in values:
        res+=val
    return res

multi_vaules = prompt_user()

total = add(multi_vaules)
print("you have "+ str(total))

You can customize val if you need to convert int or float like anything by using type casting. If you need to get int type then use int(val) to get res. But before on that you also need to declare res as int or any others

Look for integer value

def add(values):
    res=0
    for val in values:
        res+=int(val)
    return res
mhhabib
  • 2,975
  • 1
  • 15
  • 29