简体   繁体   English

python:将多个变量从 function 传递到另一个用户提示

[英]python : passing multiple variables from a function to another from a user PROMPT

How do I pass multiple (2 or more) variables from a function to another in python?如何将 function 中的多个(2 个或更多)变量传递给 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.我在互联网上进行了搜索,但我无法理解给出的大部分解释,许多示例不适合我的情况,我无法用 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?这只是无法在 python 中完成吗? Or do you have to use something like lists to pass arguments in this case?或者在这种情况下你是否必须使用类似列表的东西来传递 arguments ?

Examples I have found(yet not understood): Python — Passing Multiple Arguments https://www.geeksforgeeks.org/how-to-pass-multiple-arguments-to-function/我发现的示例(但不理解): Python — 传递多个 Arguments https://www.geeksforgeeks.org/to-function-pass

EDIT: I fixed it.编辑:我修好了。 Turns out we have to break the tuple when passing values from another function.事实证明,当从另一个 function 传递值时,我们必须打破元组。 Thank you everyone.谢谢大家。

You either want to unpack those values on function call:您要么想在 function 调用上解压缩这些值:

total = add(*two_values)

Or unpack them on assignment:或者在分配时解压它们:

val_one, val_two = prompt_user()
total = add(val_one, val_two)

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}')
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.如果您需要使用类型转换像任何东西一样转换int或 float,则可以自定义val If you need to get int type then use int(val) to get res .如果您需要获取int类型,则使用int(val)获取res But before on that you also need to declare res as int or any others但在此之前,您还需要将res声明为int或任何其他

Look for integer value寻找integer

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

I think the problem is due to the definition of arguments in method definition.我认为问题在于方法定义中 arguments 的定义。 So in general you can pass lists, functions, etc. to a function.因此,通常您可以将列表、函数等传递给 function。 In this case, I have collected the inputs in a tuple and pass the tuple to the function.在这种情况下,我将输入收集到一个元组中,并将元组传递给 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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM