所以,我對 Python 比較陌生,如果這個問題看起來很愚蠢,我提前道歉。
在我試圖制作的練習游戲中,以了解如何制作我自己的基于文本的冒險,玩家被問到他們是否會繼續,他們必須使用他們的燈籠進一步沿著隧道前進,然后說這句話;
elif nolamp and lan2 == ("use lantern") or lan2 == ("USE LANTERN") or lan2 == ("Use lantern") or lan2 == ("use LANTERN") or lan2 == ("Use LANTERN"):
litlamp = True
nolamp = False
tunnel2 = True
print ("")
print ("You light the lantern and can now see.")
print ("The tunnel is still very dark ahead.")
time.sleep(2)
print ("The walls look wet.")
time.sleep(1.6)
print ("Will you go back?")
print ("")
如果他們的燈籠沒有打開,它會這樣列印;
elif nolamp and lan2 == ("n") or lan2 == ("N") or lan2 == ("no") or lan2 == ("NO") or lan2 == ("No"):
print ("You would continue, but it's too dark and cold.")
print ("To go any farther like this would be a bad idea.")
time.sleep(2.5)
print ("Will you go back?")
上面的陳述句在單獨的 while 回圈中定義;
lanternv2 = True
tunnelv1 = True
nolamp = False
tunnel2 = False
litlamp = False
當他們的燈籠被點亮時,玩家應該繼續;
elif tunnel2 and lan2 == ("n") or lan2 == ("N") or lan2 == ("no") or lan2 == ("NO") or lan2 == ("No"):
tunnel3 = True
tunnel2 = False
print ("You continue onward.")
print ("You still see no light and the entrance is disappearing behind you.")
time.sleep(2.5)
print ("It's very very cold.")
time.sleep(1.1)
print ("Will you go back?")
然而,我的問題是,當玩家在點亮燈籠時嘗試使用“n”以外的任何其他答案時,將列印的內容就像他們的燈沒有被使用一樣。
有誰知道為什么會這樣,我需要更具體嗎?再次感謝,對不起,如果這是一個愚蠢的問題。
uj5u.com熱心網友回復:
我無法從代碼中遵循您想要的內容,但希望下面能給您一些可以遵循的想法。具體來說,當他們的燈被點亮時,您會問“除“n”之外的任何其他答案[但它會列印],就好像他們的燈沒有被使用一樣”。提供的唯一不是“否”的答案是“使用燈籠選項”,但該選項會檢查玩家是否沒有燈。只有當您回答“否”時,才會列印其他陳述句。所以在我看來,正在列印的代碼部分不包含在問題中。
我盡量保持簡單,也許它可以提供一些線索!反正我寫得很開心。
import random
# I tried to keep the code similar to yours
lanternv2 = True
tunnelv1 = True
nolamp = False
tunnel2 = False
litlamp = False
lan2 = ""
#The statements are defined above within a separate while loop;
while lan2 != "no" and lan2 != "n" and lan2 != "y":
lan2 = input("Do you want to use a lantern to enter the dark and very spooky cave?")
lan2 = lan2.lower()
if lan2 == "y":
# Player has entered the cave
litlamp = True
nolamp = False
tunnel2 = True
inCave = True #always true while in the cave
print ("")
print ("You light the lantern and can now see.")
print ("The tunnel is still very dark ahead.")
#time.sleep(2)
print ("The walls look wet.")
#time.sleep(1.6)
while(inCave):
# generates a random number 0 - 1
if(random.random() <.1):
print("Your lantern sputters and the light disappears")
litlamp = False
caveInput = input("Will you go back?").lower()
if not litlamp and (caveInput == ("n") or caveInput == ("no")):
print ("You would continue, but it's too dark and cold.")
print ("To go any farther like this would be a bad idea.")
#time.sleep(2.5)
print ()
#The player should continue when their lantern is lit with this;
elif litlamp and (caveInput == ("n") or caveInput == ("no")):
# They can only continue into the cave
print ("You continue onward.")
print ("You still see no light and the entrance is disappearing behind you.")
#time.sleep(2.5)
print ("It's very very cold.")
#time.sleep(1.1)
else: # should check if yes but this is taking too long lol
print("you flee the cave, never to return")
#break out of the while loop
inCave = false
print("You have fled the cave")
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/460410.html
上一篇:使用遞回連接兩個字串
下一篇:條件變數是否存在和不存在
