简体   繁体   中英

How to disable/restrict a line of code after it has been run? (Python)

I'm very new to coding and Python. I'm making a simple "choose your adventure" type game in Python. The whole object of the game is to enter all 4 rooms/doors and acquire all 4 digits of the secret code to enter a locked door, which leads to the treasure. This is what im using to add a digit to the code:

from random import choice

code = range(10)
my_code = []

def add_code():
    if len(my_code) < 4:
        code_digit = choice(code)
        my_code.append(code_digit)

So for every room, I have puzzles and challenges to conquer. If you complete the challenge, I have it run the add_code() function. What I want to avoid is having a user repeatedly go to the same door, complete the same challenge, and add a digit to the list, without even having to open any other door or complete any other challenge. Is there a way to make a certain line of code not run after it has already been ran once? Like, if door 1's challenge was completed and a digit was added to the code, is there a way to not let the user add another digit from door 1's add_code() function?

Associate each challenge with a boolean flag. Set the flag to True when the player finishes the challenge, and check the flag before giving the player the option to do the challenge again.

For example, if you had a "punch monkeys" quest, you might have the following flag:

monkeys_punched_yet = False

When the player punches the monkeys, you'd set

monkeys_punched_yet = True

In the monkey-punching area, you'd have a check something like this:

if monkeys_punched_yet:
    description_text = ("You see a pile of bruised and battered monkeys "
                        "in a corner of the room.")
else:
    description_text = "You see a group of unsuspecting, punchable monkeys."
    options.append("punch monkeys")

You could check to see if the new code is not already in the list of completed codes.

def add_code():
    if len(my_code) < 4:
        code_digit = choice(code)
        if ( code_digit in my_code):
           print("Already visited that room")
        else:
            my_code.append(code_digit)

Use booleans:

door1 = False
door2 = False
door3 = False
door4 = False

After you have completed a challenge in one door, set the relevant door to True . Then, you'll want to have some kind of a centre to display each door. Think in a game how you're in the centre room, and you are surrounded by four doors. Your code would be something like this:

which_door = raw_input('Which door do you want to enter? ')
if which_door == "1":
    if door1: # This is short for "if door1 is True"
        print "You have already completed this challenge."
    else: # Otherwise, it is set to False. The challenge has not been started
        do_stuff()
        door1 = True
etc for each door...

By the way, there's a simpler way to make a random code:

>>> random.sample(range(10), 4)
[3, 4, 0, 1]

What you could do is doing adding completed challenges to a list of completed challenges. Each challenge must have a unique identifier.

user.completed_challenges = set()

When opening a challenge, check if the challenge is in the list of completed challenge

if not challenge.uuid in user.completed_challenges:
   # do code

   # When everything is done add the uuid to challenges
   user.completed_challenges.add(challenge.uuid)

Also one thing you could do is to have for exemple two rooms that are the same challenge, if you do one of them. Both should be marked as "done". Then give both room the same unique identifier

It is quite simple but should do the job in your case I guess.

explanations

Instead of using booleans, I'd rather use uuids . It can be strings or whatever. It has to be an identifier that is capable to identify single challenges.. For exemple ['room1', 'room2', 'room3', 'room4']

The set prevent you from adding more than once elements. The problem with booleans is that it is not quite flexible to use. You'll have to create n variables for n challenges. It won't be very clean to add more challenge. When you have less than 5 challenges, it should work fine but if you want to add more, having a set that keep tracks of the challenges you've done is much better.

The other good thing about the set, is that you'll have a good idea which challenges the user already completed.

print "You completed challenges %s" % ', '.join(user.completed_challenges)

Whith booleans, you'll have to iterate over all hardcoded booleans which will get much much harder to handle with time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM