我正在做一个小的逃生室项目,当你完成一个动作时,它应该在完成某些动作后循环,但我的代码不会这样做。有人能帮我修好吗?这是我的密码。链接:(https://replit.com/@HoloGrain/Escape-Room-or-Bigger-Project#main.py)
inventory= []
HP=100
ImplacedAnemo= False
ImplacedGeo= False
ImplacedElectro= False
print("Type every word beggining with a capital letter")
def Help():
print("Commands:")
print("Pick Up (Item))")
print("Throw (Item) At (Other item)")
print("Press (Item)")
print("Hit (Item)")
print("Open (Place)")
print("Use (Item), (Command can only be used in battles)")
print("Use (Item) On (Item or Door(ex. Room 1 Door))")
print("Inventory", "(Opens inventory)")
print("Check HP", "(Checks HP)")
def start():
print("Welcome to HoloGrain's Escape room")
print("You are locked in Tenshukaku")
def Room1():
print("Paimon: Where are we")
print("You: I don't know")
print("Paimon: We should get out")
def Room1Command():
ChainsLocked = True
print("Room 1 Tools: Glass cage (contains sword), Paimon (If you get hungry and talks), Rock, Door (locked using a sword lock), Chains (Locked using sword lock)")
print("You're chained to the floor")
Room1Start = input("What do you want to do?: ")
if Room1Start == "Pick Up Rock":
for x in inventory:
if x == "Rock":
print("Action: Hey, don't even try")
Room1Command()
elif x == "Dull Blade":
print("Hey, don't even try")
Room1Command()
else:
inventory.append("Rock")
print("You have picked up a rock")
Room1Command()
elif Room1Start == "Throw Rock At Glass Cage":
for x in inventory:
if x == "Rock":
print("Action: You succesfully threw the rock it shatters the cage and you get the dull blade (6 DMG) and can ")
inventory.remove('Rock')
inventory.append("Dull Blade")
else:
print("You don't have a Rock to throw")
elif Room1Start == "Use Dull Blade On Chains":
for x in inventory:
if x == "Dull Blade":
if ChainsLocked == True:
ChainLocked = False
inventory.append("Chains")
print("You have unlocked the chains (Can stop one attack from an enemy and restarts mega-skills cooldown (One-time use)) and taken them")
else:
print("Action: You've done this already")
elif Room1Start == "Check HP":
print(HP)
Room1Command()
elif Room1Start == "Check Inventory":
print(inventory)
Room1Command()
elif Room1Start == "Use Dull Blade On Door":
for x in inventory:
if x == "Dull Blade":
if ChainLocked == False:
print("You've opened the door")
else:
print("That doesn't work")
else:
print("Action: Undefined action in current state")
Room1Command()
Help()
start()
Room1()
Room1Command()发布于 2022-02-02 12:10:21
您的代码中没有任何循环。它只调用所有函数一次,就结束了。
如果希望Room1Command始终运行,只需在Room1Command函数的最后一行中添加Room1Command()即可。所以看起来是这样:
def Room1Command():
ChainsLocked = True
print("Room 1 Tools: Glass cage (contains sword), Paimon (If you get hungry and talks), Rock, Door (locked using a sword lock), Chains (Locked using sword lock)")
print("You're chained to the floor")
Room1Start = input("What do you want to do?: ")
if Room1Start == "Pick Up Rock":
for x in inventory:
if x == "Rock":
print("Action: Hey, don't even try")
Room1Command()
elif x == "Dull Blade":
print("Hey, don't even try")
Room1Command()
else:
inventory.append("Rock")
print("You have picked up a rock")
Room1Command()
elif Room1Start == "Throw Rock At Glass Cage":
for x in inventory:
if x == "Rock":
print("Action: You succesfully threw the rock it shatters the cage and you get the dull blade (6 DMG) and can ")
inventory.remove('Rock')
inventory.append("Dull Blade")
else:
print("You don't have a Rock to throw")
elif Room1Start == "Use Dull Blade On Chains":
for x in inventory:
if x == "Dull Blade":
if ChainsLocked == True:
ChainLocked = False
inventory.append("Chains")
print("You have unlocked the chains (Can stop one attack from an enemy and restarts mega-skills cooldown (One-time use)) and taken them")
else:
print("Action: You've done this already")
elif Room1Start == "Check HP":
print(HP)
Room1Command()
elif Room1Start == "Check Inventory":
print(inventory)
Room1Command()
elif Room1Start == "Use Dull Blade On Door":
for x in inventory:
if x == "Dull Blade":
if ChainLocked == False:
print("You've opened the door")
else:
print("That doesn't work")
else:
print("Action: Undefined action in current state")
Room1Command()
Room1Command()通过这样做,每次用户输入命令并对其进行处理时,您的代码都将等待下一个输入。这意味着您的Room1Command函数将有一个循环。如果我对你的问题有了正确的理解,我认为这是可行的。
还请记住,名为Inventory的列表是空的,因此不能对其进行迭代。你应该先在里面放一些值。
发布于 2022-02-02 12:10:44
雾
elif Room1Start == "Throw Rock At Glass 在结尾没有一个"Room1Commande()“
发布于 2022-02-02 12:18:55
所以我看到的第一件事是,你在一个空的列表中循环--所以这是不可能的。
也许你的意思是这样的:
def Room1Command():
ChainsLocked = True
print(
"Room 1 Tools: Glass cage (contains sword), Paimon (If you get hungry and talks), Rock, Door (locked using a sword lock), Chains (Locked using sword lock)"
)
print("You're chained to the floor")
Room1Start = input("What do you want to do?: ")
if Room1Start == "Pick Up Rock":
if len(inventory) == 0 or "Rock" not in inventory:
inventory.append("Rock")
print("You have picked up a rock")
Room1Command()
else:
do_something_else()https://stackoverflow.com/questions/70955040
复制相似问题