[英]How to properly quit a program in python
我是一名中學生,我開始學習 Python 編碼。 我一直在看視頻教程,但我似乎無法弄清楚如果您鍵入 q 時如何退出游戲。 在這里,我有什么..
print('How old do you thing Fred the Chicken is?')
number = 17
Quit = q
run = 17
while run:
guess = int(input('Enter What You Think His Age Is....'))
print('How old do you thing Fred the Chicken is?')
number = 17
Quit = 'q'
run = 17
while run:
guess = int(input('Enter What You Think His Age Is....'))
if guess == number:
print('Yes :D That is his age...')
run = False
elif guess < number:
print('No, Guess a little higher...')
elif guess > number:
print('No, Guess a little lower....')
print('Game Over')
print('Press Q to Quit')
if run == False:
choice = input('Press Q to Quit')
if choice == 'q'
import sys
exit(0)
Q
作為輸入Quit = int(input('Press Q to Quit')
您要求Q
作為輸入,但只接受一個int
。 所以int
部分:
Quit = input('Press Q to Quit')
現在Quit
將是用戶輸入的任何內容,所以讓我們檢查“Q”而不是True
:
if Quit == "Q":
而不是sys.exit(0)
,你可能只是用break
結束你的 while 外觀,或者如果你在一個函數中就return
。
另外,我不建議為僅存儲用戶輸入的變量使用名稱“退出”,因為它最終會令人困惑。
請記住,縮進在 Python 中很重要,因此它必須是:
if run == False:
choice = input('Press Q to Quit')
if choice == "Q":
# break or return or..
import sys
sys.exit(0)
不過,這可能只是復制/粘貼錯誤。
我修復了縮進並刪除了一些無關的代碼(因為您復制了外部循環和一些打印語句)並得到了這個:
print('How old do you thing Fred the Chicken is?')
number = 17
run = True
while run:
guess = int(input('Enter What You Think His Age Is....t'))
if guess == number:
print('Yes :D That is his age...')
run = False
elif guess < number:
print('No, Guess a little higher...')
elif guess > number:
print('No, Guess a little lower....')
if run == False:
print('Game Over')
choice = input('Press Q to Quit')
if choice == 'q'
break
這給了我一個語法錯誤:
blong@ubuntu:~$ python3 chicken.py
文件“chicken.py”,第 23 行
如果選擇 == 'q'
^
語法錯誤:無效語法
所以 Python 說if
語句之后有問題。 如果您查看其他if
語句,您會注意到這個語句的末尾缺少:
,因此將其更改為:
if choice == 'q':
因此,隨着該更改,程序會運行,並且似乎可以執行您想要的操作。
您的說明說“按 Q 退出”,但實際上您只接受“q”退出。 您可能想同時接受兩者。 Python 有一個名為or
的運算符,它接受兩個真值( True
或False
),如果它們中的任何一個為True
則返回True
(它實際上對True
和False
之外的值做的比這更多,如果您有興趣,請參閱文檔)。
例子:
>> True or True True >>> True or False True >>> False or True True >>> False or False False
所以我們可以用if choice == "Q" or choice == "q":
來請求 Q 或 q。
另一種選擇是將字符串轉換為小寫並只檢查q
,使用if choice.lower() == "q":
。 如果choice
是 Q,它將首先將其轉換為 q(使用.lower()
),然后進行比較。
您的數字始終是17。Python有一個名為random.randint()的函數,它會給您一個隨機數,這可能會使游戲更有趣。 例如,這將使雞的年齡在 5 到 20(含)之間:
number = random.randint(5, 20)
有很多方法可以退出某些事情。 對於循環,它使用break
,而對於函數,您可以使用return
。 但是,對於程序,如果您希望在解釋完成(腳本結束)之前退出程序,則有兩種不同類型的exit()
函數。 sys.exit()
是sys
模塊的一部分, exit()
和quit()
是內置的。 但是, sys.exit()
用於不在 IDLE 中的程序(python 交互),而內置的exit()
和quit()
函數用於在 IDLE 中使用。
您可以使用break
語句跳出while
循環:
while True:
guess = int(input("...")
# ...rest of your game's logic...
# Allow breaking out of the loop
choice = input("Enter Q to quit, or press return to continue")
if choice.lower() == "q":
break
這意味着解釋器退出while
循環,繼續尋找更多要運行的命令,但到達文件末尾! Python 將自動退出 - 無需調用sys.exit
( sys.exit
,您應該很少使用sys.exit
,它實際上只是用於設置非零exit-status ,而不是用於控制程序的工作方式,例如從while
循環中轉義)
最后,您發布的代碼的主要問題是縮進 - 您需要在每行的開頭放置適當數量的空格 - 您有以下內容:
if run == False:
choice = input('Press Q to Quit')
它必須是這樣的:
if run == False:
choice = input('Press Q to Quit')
與while
、 for
、 if
等類似。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.