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

Why am I getting an error stated that k is not defined, even though i have returned it?

Why am I getting an error stated that k is not defined, even though i have returned it?

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

askpass()
print(k)

>Solution :

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

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)

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