簡體   English   中英

我的python程序不退出,一直循環運行

[英]My python program doesn't quit, keeps running loop

我正在編寫一個用戶選擇選項的簡短程序,我寫了一個 function(是/否)供用戶選擇是返回家中還是退出程序,當用戶選擇“否”時,程序應該顯示再見。 消息並退出,但循環似乎顯示再見消息,但仍提示用戶是否要退出。

這是我的 function

def exit_home():
    while True:
            user = input("home or exit? (Yes/No)").lower()
            if user == "yes":
                main()
            elif choice == "no":
                print(quit)
                quit()
                print("Bye! ")
                continue
            else:
                break
                print("enter a valid input (Yes or No)")

我得到下面的結果

home or exit? (Yes/No)no
<IPython.core.autocall.ZMQExitAutocall object at 0x7fa9c096df10>
Bye! 

home or exit? (Yes/No) 

此外,如果有一種更簡潔的方法讓用戶退出程序而不打印 <IPython.core blah blah blah> 我將不勝感激。

謝謝

您的代碼中存在幾個問題:

  1. 刪除print(quit)以擺脫<IPython.core.autocall.ZMQExitAutocall object at 0x7fa9c096df10>
  2. 使用return來打破循環而不是continue
  3. breakprint沒有意義,因為永遠不會執行print
  4. break沒有意義,因為它打破了循環,但你想進入另一個交互

更正后的代碼:

while True:
    action = input("home or exit? (Yes/No) ").lower()
    if action == "yes":
        print("call main function...")
    elif action == "no":
        print("Bye! ")
        break
    else:
        print("enter a valid input (Yes or No)")

樣本 output:

home or exit? (Yes/No) yes
call main function...
home or exit? (Yes/No) maybe
enter a valid input (Yes or No)
home or exit? (Yes/No) no
Bye! 

<IPython.core.autocall.ZMQExitAutocall object at 0x7fa9c096df10>顯示是因為您打印了 function。

這應該有效。

import sys

elif choice == "no":
     print("Bye! ")
     sys.exit()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM