make a function unavailable after it's been solved?

1 python pycharm

i am very new to python (we are using pycharm in my half-year programming class) and i am working on my final project which is a choose your own adventure kind of thing. as an example of what im asking, there is a part where you go into a crawlspace and retrieve a guide to fix something else. after you retrieve the guide, if you were to go back to the crawlspace it'd act as if you retrieved the guide again, which is what im unsure on how to solve. here's the main menu for the section (im sure there's a way to simplify all my code but we have to do it like this):

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()
Run Code Online (Sandbox Code Playgroud)

and going to the crawlspace is this:

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()

Run Code Online (Sandbox Code Playgroud)

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

Jor*_*ère 6

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

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

crawlspace()
crawlspace()
Run Code Online (Sandbox Code Playgroud)

输出:

exploring...
you already explored the crawlspace
Run Code Online (Sandbox Code Playgroud)

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

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()
Run Code Online (Sandbox Code Playgroud)

输出:

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...
Run Code Online (Sandbox Code Playgroud)