2013-05-21 3 views
0

암호를 요청하여 암호를 받으려고 시도하는 중입니다. 암호를 올바르게 입력하면 다른 일이 발생하지 않으면 어떤 일이 발생합니다. 내 코드를 시도하면 오류가 발생합니다. 라인 10, 18 :할당 오류 이전에 참조 된 변수

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() 
+0

Python2 또는 Python3? – sberry

+0

x, y 줄을 참조하려면 코드에 레이블을 붙이십시오. –

+0

나는 그것이 파이썬 3.3.0이라고 생각한다. –

답변

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 번 인쇄 할 것입니다. 또한 이 아니라 전역 변수를 사용하는 것이 더 좋을 것입니다. 암호는 askforPasswordreturn이고 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에서 뭔가를 반환합니다. 당신이 일을 끝낼 무엇이든

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() 
여기

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

코드입니다

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

, (이 표준 출력에 표시하지 않도록) 대신 input() 또는 raw_input()의 표준 파이썬 모듈 getpass를 대신 사용하십시오 :

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