我不明白quite_program = Falseand quite_program = True。他們的意思是什么?對于這段代碼,我認為它會執行代碼塊,while not quit_program但我不知道如何理解它。這意味著如果quit_program 不是False?我有點困惑,非常感謝你的幫助。
def print_menu():
print("Today's Menu:")
print(' 1) Gumbo')
print(' 2) Jambalaya')
print(' 3) Quit\n')
quit_program = False
while not quit_program :
print_menu()
choice = int(input('Enter choice: '))
if choice == 3 :
print('Goodbye')
quit_program = True
else :
print('Order: ', end='')
if choice == 1 :
print('Gumbo')
elif choice == 2 :
print('Jambalaya')
print()
uj5u.com熱心網友回復:
在您的程式中,quit_program用于根據需要停止程式的執行。最初檢查while回圈條件時,quit_program = False然后not quit_program給出True.
while 回圈一直令人興奮,直到quit_program = True. 如果quit_program = Truethennot quit_program給出False然后while回圈將結束執行(運行)。這意味著此while回圈僅在choice == 3條件為時才結束True。
因此,您的程式正在執行(運行),直到您給出inputas 3。
uj5u.com熱心網友回復:
定義了變數 quit_program = False,以便當代碼到達 while 回圈并檢查條件是否為 True 時,它??進入回圈(因為不是 False = True)。一旦進入回圈,它將繼續運行并在每次啟動時檢查此條件。如果將quit_program 更改為True,則下次在while 回圈開始時檢查條件時,它將確定它為False(不是True = False)并跳過回圈。由于回圈后沒有更多代碼,程式正常結束。
uj5u.com熱心網友回復:
這很簡單..就像在任何其他編程語言中一樣 C , CPP , JAVA ......我們經常使用'flag'對嗎?......BIngoo ...所以就像他們在這里使用了'quit_program ' 布爾型別變數.. 很明顯:
while not quit_program :
意味著在一個while回圈中還有一個'not',這意味著只要條件滿足它就會回傳false......所以while not quit_program(quit_program是false ..所以它回傳true->你可以進入回圈......那里你繼續列印里面的東西
def print_menu():
涼爽的?所以接下來,代碼繼續執行代碼中的下一組行->
choice = int(input('Enter choice: '))
if choice == 3 :
print('Goodbye')
在此之后,quit_program 如果設定為true !!! 這是捕獲-> 從現在開始->>>而不是 quit_program :將回傳 False!
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/438546.html
