[英]Python 3, Passing data from one function to another
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. 我在弄乱python,并试图制作一个简单的计算器程序。 I'm trying to pass the input values from the uInput module, to the calc module.
我正在尝试将输入值从uInput模块传递到calc模块。 It keeps saying missing two required position arguments.
它总是说缺少两个必需的位置参数。 Can you only pass one variable from a module to another module?
您只能将一个变量从一个模块传递到另一个模块吗?
A function exits at the first return statement it encounters, so return value2
is never reached. 函数在遇到的第一个return语句处退出,因此永远不会达到
return value2
。 To return multiple values use a tuple
: 要返回多个值,请使用
tuple
:
return value1, value2 #returns a tuple
Assign the returned value from uInput()
to variables inside main
: 将
uInput()
返回的值分配给main
内部的变量:
val1, val2 = uInput() #Assign using sequence unpacking
Pass these variables to calc
: 将这些变量传递给
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()
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.
当您使用return语句时,流按字面意义上是“ returns”,因此代码中的第二个return不可访问。
However, in python, you can return multiple values as a "tuple": 但是,在python中,您可以将多个值作为“元组”返回:
return value1,value2
You can return two things at once from any function, but you can only use one return statement. 您可以从任何函数一次返回两件事,但是只能使用一个return语句。 by using
return x, y
this returns a tuple (x, y)
, which you can use in your main function. 通过使用
return x, y
返回一个元组(x, y)
,您可以在主函数中使用它。
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)
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 有关更多详细信息,请查看http://docs.python.org/tutorial/controlflow.html#unpacking-argument-lists
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.