Reputation: 115
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
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
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