2013-04-14 5 views
1
Another_Mark = raw_input("would you like to enter another mark? (y/n)") 

while Another_Mark.lower() != "n" or Another_Mark.lower() != "y": 
    Another_Mark = raw_input("Do you want to input another mark? Please put either 'y' or 'n' this time") 

if Another_Mark == "y": 
    print "blah" 

if Another_Mark == "n": 
    print "Blue" 

이것은 처음 세 줄을 제외하고 사용하고있는 실제 코드가 아닙니다. 어쨌든 내 질문에 왜 while 루프는 'y'또는 'n'값을 입력해도 반복되는데, 세 번째 줄에 다른 기호를 입력 할 것인지 다시 묻는 것입니다. 나는 무한 반복되는 루프에 갇혀있다. 그것은 Another_Mark의 값이 변경 될 때 반복해서는 안 중 하나 "Y"또는 "N"파이썬 루프가 계속 반복됩니까? 왜?

+2

'하지 (a 또는 : 그리고 젖혀 OR에 (당신이 뭘 하려는지입니다)

NOT ("yes" OR "no") 

아니면 괄호에 NOT을 배포 할 수 있습니다 : 당신은 말할 수 b)'(당신이 설명하는 것)은'not a or not b' (코드가 말하는 것)와 같지 않습니다. –

답변

5

시도 : 지정된 객체가 발견되지 않는 경우

while Another_Mark.lower() not in 'yn': 
    Another_Mark = raw_input("Do you want to input another mark? Please put either 'y' or 'n' this time") 

not in 연산자는 true를 돌려 주어 반복 가능한 및 그렇지 않으면 false. 그래서 이것이 당신이 찾고있는 해결책입니다.


이것은 부울 대수 오류 때문에 발생하지 않았습니다. 근본적으로. Lattyware가 썼던 것처럼 :

하지 (A 또는 B) (당신이 무엇을 설명)하지 않는 여부가 (코드의 말씀)

>>> for a, b in itertools.product([True, False], repeat=2): 
...  print(a, b, not (a or b), not a or not b, sep="\t") 
... 
True True False False 
True False False True 
False True False True 
False False True True 
+0

좋은 해결책이지만 조금 더 설명하면이 대답이 훨씬 나아질 것입니다. –

+0

그래, 지금 편집 해 줘서 고마워! –

+0

나는 똑같지 않다는 것을 보여주는 간단한 증거 (무차별)에 편집했다. –

3

루프 로직을 ㄴ과 동일하지 않습니다 모든 것이 사실로 나온다. 입력이 "n"이면 "y"가 아니므로 사실이다. 반대로 "y"이면 "n"이 아닙니다.

이 시도 : 루핑 뒤에

while not (Another_Mark.lower() == "n" or Another_Mark.lower() == "y"): 
    Another_Mark = raw_input("Do you want to input another mark? Please put either 'y' or 'n' this time") 
+1

또는 Another_Mark.lower()! = "n"및 Another_Mark.lower()! = "y": '도 동등합니다. –

1

당신의 논리는 잘못된 것입니다. 작동해야 함 :

while Another_Mark.lower() != "n" and Another_Mark.lower() != "y": 
    Another_Mark = raw_input("Do you want to input another mark? Please put either 'y' or 'n' this time") 
1

OR 대신 AND를 사용해야합니다.

부울 논리가 분배하는 방식입니다.

(NOT "yes") AND (NOT "no") 
관련 문제