1

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!

2 Answers 2

2

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')
Sign up to request clarification or add additional context in comments.

1 Comment

ahh thank you! Yea I did return the three variables in one iteration of my code, but in the calc function I only called the getInput function, i didnt know I had to use all three. Thanks!
1

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')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.