2013-03-22 4 views
0

사용자가 실제로 프로그램을 종료하기를 원하는이 함수를 추가했습니다. 당신이 실제로 종료 할 때 작동하지만 당신이 프로그램에 반환 할 경우 그냥 문 루프 : 다른 곳에서Python 2.7 종료하기

def WantToQuit(): 
    Quit = raw_input("Please enter y if you are sure you want to quit, if not press n to return ") 
    if Quit == 'y': 
     print ('') 
    elif Quit == 'n': 
     DisplayMenu() 
     return WantToQuit() 

:

elif Choice == 'q': 
    WantToQuit() 
    raw_input('Press enter key to continue ') 
+4

다음 quit 기능은라는 상자가 나타납니다 자체적으로 재귀 적으로? –

+2

당신은 정말로 코드를 들여 줘야합니다. 그래서 return 문이나 두번째 raw_input이 어디에 속해 있는지 알 수 있습니다. –

+0

그냥 두번 호출했음을 깨달았습니다. 마지막에 어떤 함수가 반환되어야합니까 ?? – user1655562

답변

5

함께 sys.exit (0)print ('')를 교체하고 return WantToQuit()를 제거합니다.

변수에 .lower()을 적용하여 대/소문자를 구분하지 않는 것이 좋습니다.

def WantToQuit(): 
    Quit = raw_input("Please enter y if you are sure you want to quit, if not press n to return ") 
    if Quit == 'y': 
     print ('') 
    elif Quit == 'n': 
     DisplayMenu() 
    return WantToQuit() 

while(True): 
    DisplayMenu() 
    # Some logic to get input and handle it 
    # For example, something like 
    selection = raw_input("Please make a selection: ") 
    if(selection == "1"): 
     doSomething() 
    elif(selection == "2"): 
     doSomethingElse() 
    elif(selection == "q"): 
     WantToQuit() 
    else: 
     # TODO: Handle this ! 
     pass 

이것은 내가 어떻게 할 것입니다 :

1

당신이 그것을 설치를내는 방법은 다음과 더 많은 코드없이 잘못하고있는 일을 보여 어렵지만,

def WantToQuit(): 
    Quit = raw_input("Please enter y if you are sure you want to quit, if not press n to return ") 
    if Quit == 'y': 
     return true 
    elif Quit == 'n': 
     return false 
    else: 
     # TODO: Handle this ! 
     pass 

while(True): 
    DisplayMenu() 
    # Some logic to get input and handle it 
    # For example, something like 
    selection = raw_input("Please make a selection: ").lower() 
    if(selection == "1"): 
     doSomething() 
    elif(selection == "2"): 
     doSomethingElse() 
    elif(selection == "q"): 
     if(WantToQuit()): break 
    else: 
     # TODO: Handle this ! 
     pass 

def WantToQuit(): 
    Quit = raw_input("Please enter y if you are sure you want to quit, if not press n to return ") 
    if Quit == 'y': 
     sys.exit(0) 
    elif Quit == 'n': 
     return # Do nothing really 
    else: 
     # TODO: Handle this ! 
     pass 

while(True): 
    DisplayMenu() 
    # Some logic to get input and handle it 
    # For example, something like 
    selection = raw_input("Please make a selection: ").lower() 
    if(selection == "1"): 
     doSomething() 
    elif(selection == "2"): 
     doSomethingElse() 
    elif(selection == "q"): 
     WantToQuit() 
    else: 
     # TODO: Handle this ! 
     pass 

첫 번째 예는 WantToQuit 재미 있습니다

양자 택일로, 당신은 뭔가를 할 수 사용자 이 실제로을 종료할지 여부를 부울 값으로 반환합니다. 그렇다면 무한 루프가 끊어지고 프로그램이 자연적으로 종료됩니다.

두 번째 예제는 WantToQuit 함수 내에서 종료를 처리하고 sys.exit()을 호출하면 즉시 종료됩니다.

두 가지가 실제로 사용되지만 첫 번째 방법이 좋습니다.

0

새 기능을 만드는 대신 quit()이라는 이미 정의 된 기능을 사용할 수 있습니다. 전화 당신의`WantToQuit() '함수에 대한 이유가

[에]를

quit()

[아웃]

Your program is still running! 
Do you want to kill it? 
    Yes No