why am i getting an error that k is not defined even though i have returned it?

EDIT: I’ve missed the recursive askpass() when reading the code, if you entered the wrong password you would go into another call of the same function but that call’s return doesn’t get assigned to password. Now I don’t recommend using recursion either in here because you just keep on adding up functions call, but I recommend doing this with a while loop like this:

def askpass():
    password = ""
    while password != "root":
        password = pyautogui.password(text='Enter password for the database:', title='', default='', mask='*')
    k=password
    return k

OLD:

The correct syntax for this is

import pyautogui
def askpass():
    password = pyautogui.password(text='Enter password for the database:', title='', default='', mask='*')
    if password=='root':
        print('password correct')
    else:
        password=askpass()
    k=password
    return k

k = askpass()
print(k)

or

print(askpass())

When you return a variable, it returns it’s value that you can then get like written above.

(That’s how you can imagine it, but in python what actually happens the function returns a reference to a object that can then be assigned to a variable)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top