在運行時
代碼以一種不尋常的方式運行。當我說啟動時,它顯示我的汽車已準備好出發但是當我說再次啟動時,它直到顯示相同的輸出我希望代碼顯示我第二次說啟動時汽車已經啟動
command = ""
while command != "quit":
unit = input("> ").lower()
if unit == "start":
print ("car started ... ready to go")
elif unit == "stop":
print("car stopped")
elif unit == "help":
print("""
start - start the car
stop - stop the car
quit - exit
""")
elif unit == "quit":
break
else:
print("sorry I don't understand")
uj5u.com熱心網友回復:
您將需要一些狀態來存盤汽車當前所處的狀態,以實作這一點。在您的情況下,您可以簡單地使用car_running相應設定的標志。
car_running = False
command = ""
while command != "quit":
unit = input("> ").lower()
if unit == "start":
if car_running:
print("car has already been started")
else:
print("car started ... ready to go")
car_running = True
elif unit == "stop":
if car_running:
print("car stopped")
car_running = False
else:
print("car is not running")
elif unit == "help":
print(""" start - start the car stop - stop the car quit - exit """)
elif unit == "quit":
break
else:
print("sorry I don't understand")
如果您有多個州可以使用汽車,則可以使用enum。
你的建筑本質上是一臺有限狀態機。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/460679.html
