我是 Python 的初學者。我正在嘗試撰寫一個文本冒險游戲,提示用戶在給定不同選項的情況下探索房間。用戶輸入“1”后,我希望游戲通過另一個if陳述句輸入更多的選擇。我該怎么做?如果這種方式不正確,我應該改用什么功能?我嘗試在其中放入另一個 if 陳述句,但這會導致程式產生不同的結果,例如輸出代碼的不同部分而不是我想要的部分。這是我現在的代碼:
Name = input("What is your name, visitor?")
print(Name (", you are being watched. Proceed carefully. A breeze of howling wind enters the room. Within the echo, something reaches out to you and offers a candle. Do you want to light the candle?"))
print("1 for YES")
print("2 for NO")
try:
Choice = int(input("What do you choose?"))
print("Choice:", Choice)
except ValueError:
print("Please input 1 or 2 only...")
Choice = int(input("What do you choose?"))
if Choice == 1 :
print("A flickering candlelight bursts forth. You are blinded momentarily. When your eyes adjust, you see a table, drawer, and lamp in the room. You can check:")
print("1 for Table")
print("2 for Drawer")
print("3 for Lamp")
if Choice == 2 :
print("You sit in silence, wondering what to do. Without sight, you're losing options. Eventually, you muster up the courage to stand up. You can't hear your own steps. Fear climbs up your throat. The floor gives way under your feet. You are swallowed by the darkness. GAME OVER.")
quit()
if not 1:
print("Please enter either 1 or 2.")
if not 2:
print("Please enter either 1 or 2")```
uj5u.com熱心網友回復:
此代碼有幾個問題,但要解決您的問題:嵌套if在外部if:
if Choice == 1 :
print("A flickering candlelight bursts forth. You are blinded momentarily. When your eyes adjust, you see a table, drawer, and lamp in the room. You can check:")
print("1 for Table")
print("2 for Drawer")
print("3 for Lamp")
Choice = int(input("What do you choose?"))
if Choice == 1:
# Do table stuff
elif Choice == 2:
# Do drawer stuff
elif Choice == 3:
# Do lamp stuff
else:
# Handle error
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/496717.html
上一篇:DAXMeasure,多標準切換
