簡體   English   中英

Python 打破 while 循環

[英]Python Breaking a While Loop

我對 Python 有點陌生,對這個網站也是新手。 我想要任何有關我的代碼的幫助。 當用戶輸入一個值時,我試圖打破循環,但我遇到了問題。 我正在為我的學校做一個聊天機器人項目。

while ans:
        user_input = input("How are you?: (or press enter to quit) ")
        user_input = ''.join(ch for ch in user_input if ch not in exclude)
        user_words = user_input.split()

如果您正在尋找任何輸入,那么您將不需要 while 循環,Python 將在等待用戶輸入時暫停程序。

user_input = input("How are you?: (or press enter to quit) ")
user_input = ''.join(ch for ch in user_input if ch not in exclude)
user_words = user_input.split()

或者,如果您想等待特定值,則需要設置一個條件來中斷循環。

ans = True
while ans != "Quit":
    user_input = input("How are you?: (or press enter to quit) ")
    user_input = ''.join(ch for ch in user_input if ch not in exclude)
    user_words = user_input.split()
    if user_input == "Quit":
        ans = "Quit"

或者

while ans:
    user_input = input("How are you?: (or press enter to quit) ")
    user_input = ''.join(ch for ch in user_input if ch not in exclude)
    user_words = user_input.split()
    if user_input == "Quit":
        break

暫無
暫無

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

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