Qman485
Qman485

Reputation: 115

getting input in a function and calling it in another function

how would this work? Lets say I have a function called getInput that gets three numbers based on user input

def getInput():
    num1 = int(input("please enter a int"))
    num2 = int(input("please enter a int"))
    num3 = int(input("please enter a int"))

how would I use this function in another function to do checks regarding the input? For example

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

thanks!

Upvotes: 1

Views: 211

Answers (2)

A. Apola
A. Apola

Reputation: 131

Use an array for scalability. You may one day need to return 1000 values. Get the three numbers, place them in an array and return them as follows:

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; 

Now get the returned data and use it as follows:

 def calculation():
    getInput();
    if num_list[1] > (num_list[2] * num_list[3]):
        print('Correct')

Upvotes: 1

EngineerCamp
EngineerCamp

Reputation: 671

You need to return the variables (num1, num2, num3) from the getInput function.

Like this:

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

then you can do:

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

Upvotes: 2

Related Questions