2012-12-28 6 views
0

Im는 Python과 프로그래밍에 새로운 것이므로 일반적으로 간단한 while 루프를 사용하면서이 문제를 보았습니다. 루프는 두 가지 가능한 암호를 평가하기 위해 입력을받습니다.Python : while while 루프 오류

print('Enter password') 
    passEntry = input() 

    while passEntry !='juice' or 'juice2': 
     print('Access Denied') 
     passEntry = input() 
     print(passEntry) 

    print('Access Granted') 

유효하지 않은 주스 또는 juice2를 수락하지 않는 것 같습니다.

while passEntry !='juice' : 

잘 작동 : 반면,

while passEntry != 'juice' : 

가 작동하지 않습니다

또한처럼 한 비밀번호를 수용. 나는이 문제들에 대한 이유를 찾지 못하는 것 같다. (후자 둘 사이의 유일한 차이점은 = 뒤의 공간이다). 어떤 도움을 주셔서 감사합니다.

+0

이 오류의 이유가되지 않을 수 있습니다 것 같아요 :

이 적절한 코드가 될 것입니다! –

답변

7

먼저 파이썬의 getpass 모듈을 사용하여 이식 가능한 비밀 번호를 얻어야합니다. 예를 들어 다음

import getpass 
passEntry = getpass.getpass("Enter password") 

, 당신이 작성한 코드는 while 루프 가드 :

while passEntry != 'juice' or 'juice2': 

가드 표현

(passEntry != 'juice') or 'juice2' 
와 while 루프로 파이썬 인터프리터에 의해 해석됩니다

passEntry이 "juice"와 같은지 여부에 관계없이 "juice2"는 부울로 해석 될 때 true로 간주되기 때문에 항상 true입니다.

파이썬에서 멤버십을 테스트하는 가장 좋은 방법은 목록이나 집합 또는 튜플과 같은 다양한 데이터 유형에 사용되는 in operator을 사용하는 것입니다. 예를 들어, 목록 :

while passEntry not in ['juice', 'juice2']: 
0

이 방법이 효과가 있습니까?

while passEntry !='juice' and passEntry !='juice2': 
1

방법에 대해 :

while passEntry !='juice' and passEntry!= 'juice2': 

raw_input() 대신 input()를 사용하고 계십니까?

input()은 마치 입력이 파이썬 코드 인 것처럼 평가합니다.

+0

예 첫 번째 부분에서는이 오류가 발생했거나 및를 사용하여 논리적 오류가 발생했습니다. 고마워 – baker641

+0

raw_input() didnt는 작동하는 것처럼 보입니다. – baker641

3

당신은

while passEntry not in ['juice' ,'juice2']: 
1

passEntry !='juice' or 'juice2'(pass != 'juice') or ('juice2')을 의미 할 수 있습니다. "juice2"은 비어 있지 않은 문자열이므로 항상 참입니다. 따라서 귀하의 상태는 항상 사실입니다.

passEntry != 'juice' and passEntry != 'juice2' 또는 더 멋지게 passEntry not in ('juice', 'juice2')하고 싶습니다.

0

while 문을 작성하는 중 오류가 발생했습니다.

while passEntry !='juice' or 'juice2': 

파이썬 인터프리터에서 읽을 때 항상 그 라인이 참입니다. 또한 대신 :

passEntry = input() 

사용 :

passEntry = raw_input() 

input 파이썬 2 evals 귀하의 의견을 (파이썬 3 사용하지 않는 경우).

print('Enter password') 
passEntry = raw_input() 

while passEntry != 'juice' and passEntry != 'juice2': 
    print('Access Denied') 
    passEntry = raw_input() 
    print(passEntry) 

print('Access Granted') 
+0

좋아요. python3을 실행 중입니다. 그래서 raw_input()이 나에게 문제를 일으킨 이유가 여기에 있습니다. 감사 – baker641