은, 일의 당신이 얻을 오류를 살펴 보자하지 않는 이유를 요구하고 있기 때문에 :
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
에서 뭔가를 반환합니다. 당신이 일을 끝낼 무엇이든
Python2 또는 Python3? – sberry
x, y 줄을 참조하려면 코드에 레이블을 붙이십시오. –
나는 그것이 파이썬 3.3.0이라고 생각한다. –