Chris Pickett
Chris Pickett

Reputation: 57

Variable referenced before assignment error

I am trying to make my code to ask for a password and receive it, if the password is correct something will happen if not something else will happen, when I tried my code it gives me an error on line 10, and 18:

if (password == 'xxxx'):
UnboundLocalError: local variable 'password' referenced before assignment

Here is the code:

import random

def askforPassword():
    print('Welcome to the Machine!')
    print('Enter the password: ')
    password = input()

def getPassword():
    while passwordTry != 0:
       if (password == 'xxxx'):
        print('Correct')
       else:
        passwordTry -= 1
        print('INCORRECT!')

passwordTry = 5
askforPassword()
getPassword()

Upvotes: 0

Views: 1145

Answers (4)

Velimir Mlaker
Velimir Mlaker

Reputation: 10985

Whatever you end up doing, instead of input() or raw_input(), be sure to use the standard Python module getpass instead (so that it doesn't echo to stdout):

import getpass
foo = getpass.getpass('Enter the password: ')
print('You typed: %s'%foo)

Upvotes: 0

abarnert
abarnert

Reputation: 366103

Since you're asking why it doesn't work, let's look at the error you get:

Traceback (most recent call last):
  File "pw.py", line 18, in <module>
    getPassword()
  File "pw.py", line 10, in getPassword
    if (password == 'xxxx'):
UnboundLocalError: local variable 'password' referenced before assignment

What it's saying is that you are trying to access a local variable 'password', and you haven't created any such local variable.

If you want to use a global variable, say so explicitly:

def getPassword():
    global password
    while passwordTry != 0:
       if (password == 'xxxx'):
        print('Correct')
       else:
        passwordTry -= 1
        print('INCORRECT!')

But this still won't work, because nobody's setting that global variable, either. You need to change askforPassword too:

def askforPassword():
    global password
    print('Welcome to the Machine!')
    print('Enter the password: ')
    password = input()

This still has a lot of problems. For example, you only call askforPassword once, not once each time through the loop, so it's just going to ask once and then print INCORRECT! 5 times. Also, it would be much better to not use global variables—has askforPassword return the password, and store it in a local variable in getPassword.

def askforPassword():
    print('Welcome to the Machine!')
    print('Enter the password: ')
    password = input()
    return password

def getPassword():
    while passwordTry != 0:
       password = askforPassword()
       if (password == 'xxxx'):
        print('Correct')
       else:
        passwordTry -= 1
        print('INCORRECT!')

And you probably want to return something from getPassword too, so whoever calls it knows whether you succeeded or failed.

Upvotes: 5

perreal
perreal

Reputation: 98118

import random
password=-1
passwordTry=5

def askforPassword():
    global password                    # define as global
    print('Welcome to the Machine!')
    print('Enter the password: ')
    password = raw_input()             # python < 3 ? use raw_input

def getPassword():
    global password                    # not func. local
    global passwordTry                 # not local, global
    while passwordTry != 0:
       askforPassword()                # ask in each iteration
       if (password == 'xxxx'): 
        print('Correct')
        break                          # don't ask if correct
       else:
        passwordTry -= 1               # not password, passwordTry
        print('INCORRECT!')

getPassword()

Upvotes: 1

sberry
sberry

Reputation: 132138

You might want to think about changing up the logic slightly to something like this:

import random

def askforPassword():
    print('Welcome to the Machine!')
    print('Enter the password: ')
    password = input()
    return password

def getPassword():
    passwordTry = 5
    while passwordTry:
       if (askforPassword() == 'xxxx'):
            print('Correct')
            break
       else:
            passwordTry -= 1
            print('INCORRECT!')

getPassword()

Upvotes: 3

Related Questions