下面是我的代碼片段。假設play_again()while 回圈內部的輸出回傳 False。那么,為什么我的while回圈一直在回圈呢?有什么我不知道的概念嗎?
game_list = ['0','1','2']
while True:
position = myfunc()
replacement(game_list,position)
play_again()
print(game_list)
uj5u.com熱心網友回復:
這是 b/cwhile True除非您使用break在回圈外中斷并繼續代碼的關鍵字,否則不會結束。將while True永遠不會結束
while 回圈
while (condition):
#code
在條件為 之前永遠不會結束False,女巫對于True條件永遠不會為真。
你的代碼應該是:
game_list = ['0','1','2']
while True:
position = myfunc()
replacement(game_list,position)
if not play_again():
break
print(game_list)
或者你可以這樣做:
game_list = ['0','1','2']
while play_again():
position = myfunc()
replacement(game_list,position)
print(game_list)
uj5u.com熱心網友回復:
此代碼應該可以作業:
while (play_again()):
position = myfunc()
replacement(game_list,position)
您應該知道,在 Python 中,一個while回圈(就像在其他所有編程語言中一樣)采用一個“引數”,即 acondition型別bool:
while (i > 3): # i>3 is a boolean condition
...
實際上這相當于
while (True): # the loop continues until condition is False, so in this case it will never stop
if (i > 3):
break
在 Pythonbreak中是一個讓你退出回圈的關鍵字。
然后,正如您可能理解的那樣,此代碼等效于此答案中的第一個片段:
while (True):
position = myfunc()
replacement(game_list,position)
if (not play_again()):
break
uj5u.com熱心網友回復:
而 True 將一直運行,直到您決定中斷。
game_list = ['0','1','2']
while True:
position = myfunc()
replacement(game_list,position)
play_again = input("do you want to play again?")
if play_again == 'y':
play_again()
else:
break
print(game_list)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/418632.html
標籤:
