0

我正在尝试让我的代码要求输入密码并接收它,如果密码正确,则会发生某些事情,如果不是,则会发生其他事情,当我尝试我的代码时,它在第 10 行和第 18 行给我一个错误:

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

这是代码:

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()
4

4 回答 4

5

既然你问为什么它不起作用,让我们看看你得到的错误:

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

它的意思是您正在尝试访问一个局部变量'password',而您还没有创建任何这样的局部变量。

如果要使用全局变量,请明确说明:

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

但这仍然行不通,因为也没有人设置该全局变量。你也需要改变askforPassword

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

这仍然存在很多问题。例如,您只调用askforPassword一次,而不是每次循环调用一次,所以它只会询问一次,然后打印INCORRECT!5 次。此外,最好不要使用全局变量——拥有askforPassword return密码,并将其存储在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!')

而且你可能也想从那里返回一些东西getPassword,所以任何调用它的人都知道你是成功还是失败。

于 2013-05-21T22:18:21.837 回答
3

您可能想考虑将逻辑稍微更改为如下所示:

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()
于 2013-05-21T22:08:19.237 回答
1
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()
于 2013-05-21T22:10:20.047 回答
0

无论你最终做什么,而不是input()or raw_input(),请务必使用标准 Python 模块getpass(这样它就不会回显到标准输出):

import getpass
foo = getpass.getpass('Enter the password: ')
print('You typed: %s'%foo)
于 2013-05-21T23:08:11.970 回答