首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >脚本不在python中循环

脚本不在python中循环
EN

Stack Overflow用户
提问于 2022-02-02 11:46:02
回答 3查看 51关注 0票数 -1

我正在做一个小的逃生室项目,当你完成一个动作时,它应该在完成某些动作后循环,但我的代码不会这样做。有人能帮我修好吗?这是我的密码。链接:(https://replit.com/@HoloGrain/Escape-Room-or-Bigger-Project#main.py)

代码语言:javascript
复制
    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()
EN

回答 3

Stack Overflow用户

发布于 2022-02-02 12:10:21

您的代码中没有任何循环。它只调用所有函数一次,就结束了。

如果希望Room1Command始终运行,只需在Room1Command函数的最后一行中添加Room1Command()即可。所以看起来是这样:

代码语言:javascript
复制
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的列表是空的,因此不能对其进行迭代。你应该先在里面放一些值。

票数 0
EN

Stack Overflow用户

发布于 2022-02-02 12:10:44

代码语言:javascript
复制
elif Room1Start == "Throw Rock At Glass 

在结尾没有一个"Room1Commande()“

票数 0
EN

Stack Overflow用户

发布于 2022-02-02 12:18:55

所以我看到的第一件事是,你在一个空的列表中循环--所以这是不可能的。

也许你的意思是这样的:

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70955040

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档