コードを追加しないと、何が間違っているのかを示すのは難しいですが、次のように設定されていると思います。
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
これは私がそれをする方法です:
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
に終了するかどうかにかかわらず、関数がブール値を返します。その場合、無限ループが中断され、プログラムは自然に終了します。
2番目の例では、関数内の終了を処理し、すぐに終了するWantToQuit
ように呼び出します。sys.exit()
どちらも実際に使用されていますが、おそらく最初の方が望ましいでしょう。