2017-11-13 4 views
1

은 파이썬 3을 시도하고 잠시 루프와 연습을하고 있지만 변수가 기본 Ywhile 루프가 오 탐지를 반환하는 이유는 무엇입니까?

#!/usr/bin/python3 

elements = []; 
copy = []; 
rot=0; 
op='y'; 
ap='y'; 

while op is 'y' or 'Y': 

    elements = []; 
    a = int(input("How many elements? ")); 
    for i in range(a): 
     elements.append(input("Enter element " + str(i+1) + " ")); 

    while ap is 'y' or 'Y': 
     rot = int(input("How many element to rotate? ")); 
     print(elements); 
     copy = elements[-rot:] + elements[:-rot]; 
     elements = copy; 
     print(elements); 
     ap = input("Rotate again? "); 

    op = input("Create a new rotatrix? "); 
+0

또한 https://stackoverflow.com/questions/1504717/why-does-comparing-strings-in-python-using-either-or-is-sometimes-produce – vaultah

+0

이 중 하나를합니까 대답에는 OR 연산자가 포함됩니까? – userDepth

+0

예, 첫 번째 링크를 참조하십시오. – vaultah

답변

0

또는 운영자는 별도의 값으로 설정 될 때 루프는 끝나지 않는다 귀하의 테스트 조건에서 당신이 의도대로 작동하지 않습니다. 이 시도 :

elements = [] 
copy = [] 
rot=0 
op='y' 
ap='y' 

while op == 'y' or op == 'Y': 

    elements = [] 
    a = input("How many elements? ") 
    for i in range(a): 
     elements.append(input("Enter element " + str(i+1) + " ")) 

    while ap == 'y' or ap == 'Y': 
     rot = input("How many element to rotate? ") 
     print(elements) 
     copy = elements[-rot:] + elements[:-rot] 
     elements = copy 
     print(elements) 
     ap = raw_input("Rotate again? ") 

    op = raw_input("Create a new rotatrix? ") 
+0

제안 해 주셔서 감사합니다. @ juanpa.arrivillaga 당신이 맞습니다. – Robbie

+0

약간의 설명을 추가 할 수 있습니까? "또는 연산자가 여러 변수를 사용하지 않습니다 ..." – userDepth

+0

나는 문장의 끝에서 세미콜론을 제거하는 자유를 취했습니다. 문법적으로 유효하지만, 필요하지 않다면 (예를 들어,'my_list.append (x); my_list.append (y)'와 같은 두 줄로 분리 된 문장을 사용하지 마십시오. –