2013-08-04 5 views
-2

이것은 파이썬입니다.Python : 내 프로그램이 의도 한대로 작동하지 않습니까?

안녕하세요, 내 프로그램이 완벽하게 실행되고 오류없이 컴파일되지만 아래 두 번째 옵션을 실행할 때 아무 것도하지 않습니다. '재귀'기능은 실행되지 않고 인쇄되는 것은 "옵션 선택 :"입니다. 들여 쓰기가 어딘가에 없거나 무언가를 포함하는 것을 잊었습니까?

selection = 0 

while selection != 3: 

    #This is printed out infinitely. 
    selection = int(input("Select an option: ")) 

#Second Option 
elif selection == 2: 
    def recursion(): 
     myString = str(input("Enter your string: ")) 
     if not myString.replace('()', ''): 
      print("The string is even.") 
     else: 
      print("The string is not even.") 
+1

을; 그것은'elif'에'SyntaxError : invalid syntax'을줍니다. – Blair

답변

1

여기 일부 구조를 오해하고 있다고 생각합니다.

while selection != 3selection == 3까지 반복적으로 계속 실행되는 아래의 코드 (들여 쓰기)를 의미합니다.

elif은 while 구조에서는 사용되지 않습니다. 조건부 구조에 사용됩니다 (예 : if/elif/else).

당신은 아마이 의미 :이 코드는 실행되지 않습니다

selection = 0 
def recursion(): 
    myString = str(input("Enter your string: ")) 
    if not myString.replace('()', ''): 
     print("The string is even.") 
    else: 
     print("The string is not even.") 

while selection != 3: 
    selection = int(input("Select an option: ")) 
    if selection == 2: 
     recursion() 
관련 문제