解决后使功能不可用?

时间:2019-12-19 18:09:38

标签: python pycharm

我对python非常陌生(我们在我的半年编程课程中使用pycharm),并且我正在完成我的最终项目,这是一种选择自己的冒险方式的事情。作为即时消息询问的一个示例,您可以在其中一部分进入一个爬网空间并检索一个指南以修复其他问题。在检索指南之后,如果要返回到爬网空间,则它的作用就像再次检索指南一样,这是不确定如何解决的问题。这是该部分的主菜单(肯定有一种方法可以简化我的所有代码,但是我们必须这样做):

def left_path():
    left=input("""What will you do?
[1] Inspect computer
[2] Inspect window
[3] Inspect crawlspace
[4] Check notepad
Type here: """)
    if left=="1":
        computer()
    elif left=="2":
        window()
    elif left=="3":
        crawlspace()
    elif left=="4":
        print(inventory)
    else:
        print("That's not an option. Try again.")
        left_path()

然后转到爬网空间:

def crawlspace():
    print("You get on the floor and crawl into the crawlspace.")
    #sleep(3)
    print("You can't see, but you feel around and find a paper.")
    #sleep(3)
    print("You leave the crawlspace and look at the paper.")
    #sleep(3)
    print("It appears to be a guide to the wires...")
    #sleep(3)
    print("...but there's something written in the corner as well.")
    #sleep(3)
    print("You decide to write it down.")
    inventory.add_item(Item('5##, #△#, C##'))
    left_path()

在其他部分可以找到相同的密码,但是我只需要显示一个即可传达我的意思。希望这已经足够清楚了,香港专业教育学院从未在这里问过任何问题。基本上,如果您再次尝试选择它,我只是希望它像“您已经探索了爬网空间”。我肯定这是一个非常简单的修复程序,但是我又是新人,知道的很少。作为一个不熟悉time.sleep的旁注,主题标签只是为了让我可以加快速度来确保一切正常。

2 个答案:

答案 0 :(得分:6)

您需要将该状态保存在某处。您可以使用全局变量,但是在我看来,这只会污染您的代码。我可能更喜欢将状态绑定到函数本身。例如

def crawlspace():
    explored = getattr(crawlspace, 'explored', False)
    if explored:
        return print('you already explored the crawlspace')
    crawlspace.explored = True
    print('exploring...')

crawlspace()
crawlspace()

输出:

exploring...
you already explored the crawlspace

编辑:您甚至可以使用带有内部包装的简单装饰器来避免多余的复制粘贴:

from functools import wraps

def to_explore_only_once(func):
    @wraps(func)
    def inner(*args, **kwargs):
        if getattr(inner, 'explored', False):
            return print(f'you already explored the {func.__name__}')
        inner.explored = True
        return func(*args, **kwargs)
    return inner

@to_explore_only_once
def crawlspace():
    print('exploring the crawlspace...')

@to_explore_only_once
def forest():
    print('exploring the forest...')

@to_explore_only_once
def city():
    print('exploring the city...')

crawlspace()
crawlspace()
crawlspace()
forest()
forest()
forest()
city()
city()
city.explored = False
city()

输出:

exploring the crawlspace...
you already explored the crawlspace
you already explored the crawlspace
exploring the forest...
you already explored the forest
you already explored the forest
exploring the city...
you already explored the city
exploring the city...

答案 1 :(得分:0)

您应该以某种方式跟踪系统状态。对于这个简单的问题,一个变量就足够了。

让我们这样说:
在初始化游戏的位置添加变量声明visited = {},然后将var visited传递给所有函数。然后crawlspace变为:

def crawlspace(visited):
    # ... story here ...
    print("You decide to write it down.")
    inventory.add_item(Item('5##, #△#, C##'))
    visited['crawlspace'] = True
    left_path(visited)

def left_path(visited):
    left=input("""What will you do?
[1] Inspect computer
[2] Inspect window
[3] Inspect crawlspace
[4] Check notepad
Type here: """)
    if left=="1":
        computer(visited)
    elif left=="2":
        window(visited)
    elif left=="3":
        if not visited.get("crawlspace", None):
            crawlspace(visited)
        else:
            print("You don't find anything new.")
    elif left=="4":
        print(inventory)
    else:
        print("That's not an option. Try again.")
        left_path(visited)