Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Passing input between two functions

Need to write a code that satisfies this requirement: "Your program will contain two functions. The first should be a function that asks the user for two numbers and passes those numbers to main() which passes them to a second function. The second function should multiply those numbers together. Make sure to include a caller after and a docstring in all of your functions."
Here is what I have so far, getting an error saying my x-value is not defined.

def main():
    
    def multiplicationfunc(x,y):
        '''this function multiplies the user input'''
        z = x * y
        return z

    def user_input():
        '''recieve two integers that will be used to multiply'''
        x = eval(input("enter the first integer:"))
        y = eval(input("enter the second integer:"))
        return x,y

    
    multiplicationfunc(x,y)

user_input()
main()

How do I fix it within the question?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If I understand correctly, here is a fix to your issue:

def multiplicationfunc(x,y):
    '''this function multiplies the user input'''
    return x * y

def main(x, y):
    return multiplicationfunc(x, y)
def user_input():
    '''recieve two integers that will be used to multiply'''
    x = int(input("enter the first integer:"))
    y = int(input("enter the second integer:"))
    return main(x, y)
print(user_input())

What I am doing here is first defining the multiplicationfunc() function, that takes two parameters, x and y, and then returning the product of them. Then I am defining the main function which also takes two parameters and then calls the multiplicationfunc() with the two parameters. Then I make the user_input() function that with take two integers and then return the result from the main() function. Then I just print what user_input() returns.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading