Search code examples
pythonfunctionreturn

Why is the return function not passing the variables to the next function?


I'm trying to get rid of the error without moving the input function out of the userInput function. I expected the return function to remove the NameError. I looked all over stack exchange and in my textbook to figure out this problem.

def userInput():
    a = float(input("Enter a: "))
    b = float(input("Enter b: "))
    return (a,b)

def printFunction(a2,b2):
    print(a2)
    print(b2)

def main():
    userInput()
    printFunction(a2,b2)
    
main()

NameError: name 'a2' is not defined


Solution

  • Functions return values, not variables. The names a and b are only defined in userInput: you need to receive the values returned by userInput in variables defined in main.

    def main():
        x, y = userInput()
        Printfunction(x, y)