Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在一个函数中获取输入并在另一个函数中调用_Python_Function_Input - Fatal编程技术网

Python 在一个函数中获取输入并在另一个函数中调用

Python 在一个函数中获取输入并在另一个函数中调用,python,function,input,Python,Function,Input,这是怎么回事?假设我有一个名为getInput的函数,它根据用户输入获取三个数字 def getInput(): num1 = int(input("please enter a int")) num2 = int(input("please enter a int")) num3 = int(input("please enter a int")) 如何在另一个函数中使用此函数来检查输入?比如说 def calculation(): getInput()

这是怎么回事?假设我有一个名为getInput的函数,它根据用户输入获取三个数字

def getInput():
    num1 = int(input("please enter a int"))
    num2 = int(input("please enter a int"))
    num3 = int(input("please enter a int"))
如何在另一个函数中使用此函数来检查输入?比如说

def calculation():
    getInput()
    if num1 > (num2 * num3):
        print('Correct')

谢谢

您需要从
getInput
函数
返回变量(
num1
num2
num3

像这样:

def getInput():
   num1 = int(input("please enter a int"))
   num2 = int(input("please enter a int"))
   num3 = int(input("please enter a int"))
   return num1, num2, num3
然后你可以做:

def calculation():
    num1, num2, num3 = getInput()
    if num1 > (num2 * num3):
        print('Correct')

使用阵列实现可伸缩性。您可能有一天需要返回1000个值。获取三个数字,将它们放入一个数组中并按如下方式返回:

num_list = [];
i = 3;
temp = 0;
while i > 0:
     temp = int(input("please enter a int"));
     num_list.append(temp);
     temp=0;
     i--;
return num_list; 
 def calculation():
    getInput();
    if num_list[1] > (num_list[2] * num_list[3]):
        print('Correct')
现在获取返回的数据并按如下方式使用:

num_list = [];
i = 3;
temp = 0;
while i > 0:
     temp = int(input("please enter a int"));
     num_list.append(temp);
     temp=0;
     i--;
return num_list; 
 def calculation():
    getInput();
    if num_list[1] > (num_list[2] * num_list[3]):
        print('Correct')

啊,谢谢你!是的,我确实在代码的一次迭代中返回了三个变量,但是在calc函数中,我只调用了getInput函数,我不知道我必须使用这三个变量。谢谢