Help with lists


#1
print("welcome to my text based game")
import time



def game_intro():
    print("Harrison's text based game")
    print("welcome, player, what is your UserName?")
    name = input()
    level_1(name)
    
def game_over():
    print("***GAME OVER***")
    print("""            __________
                      .~#########%%;~.
                     /############%%;`\
                    /######/~\/~\%%;,;,\
                   |#######\    /;;;;.,.|
                   |#########\/%;;;;;.,.|
          XX       |##/~~\####%;;;/~~\;,|       XX
        XX..X      |#|  o  \##%;/  o  |.|      X..XX
      XX.....X     |##\____/##%;\____/.,|     X.....XX
 XXXXX.....XX      \#########/\;;;;;;,, /      XX.....XXXXX
X |......XX%,.@      \######/%;\;;;;, /      @#%,XX......| X
X |.....X  @#%,.@     |######%%;;;;,.|     @#%,.@  X.....| X
X  \...X     @#%,.@   |# # # % ; ; ;,|   @#%,.@     X.../  X
 X# \.X        @#%,.@                  @#%,.@        X./  #
  ##  X          @#%,.@              @#%,.@          X   #
, "# #X            @#%,.@          @#%,.@            X ##
   `###X             @#%,.@      @#%,.@             ####'
  . ' ###              @#%.,@  @#%,.@              ###`"
    . ";"                @#%.@#%,.@                ;"` ' .
      '                    @#%,.@                   ,.
      ` ,                @#%,.@  @@                `
                          @@@  @@@  """)
    time.sleep(1)
    print("restart?")
    restart = input().lower()
    if restart == "yes":
        game_intro()
    elif restart == "no":
        exit()
    else:
        print("please answer yes or no!!")
        game_over()
        

def level_1(name):
    print("level 1")
    time.sleep(2)
    print("Welcome to your worst nightmare",name)
    time.sleep(2)
    print("You wake up feeling dizzy, you are in a")
    time.sleep(2)
    print("lot of pain.")
    time.sleep(2)
    print("you notice you have no idea where you are")
    time.sleep(2)
    print("your heart starts racing")
    time.sleep(2)
    print("you look around and notice the room has various items")
    time.sleep(2)
    print("you reach out and pick up a piece of paper, with the code written 9878")
    time.sleep(2)
    print("you see the door as a combination lock that accepts a 4-digit code")
    time.sleep(2)
    print("you have a choice,")
    time.sleep(2)
    print("1) input code into the door")
    print("2) look further around the room")
    choice = int(input())
    if choice == 1:
        print("the door open's!")
        print("you will start level 2 now!")
        level_2(name)
    elif choice == 2:
        print("you start to have a further look around and see a stuffed lion")
        print("in the corner of the room.")
        time.sleep(2)
        print("you walk towards it,")
        print("you touch it's fur.")
        time.sleep(2)
        print("you notice a note attached to the lion")
        time.sleep(2)
        print("the note says 'YOU SHOULD HAVE ESCAPED WHILE YOU COULD' then...")
        time.sleep(4)
        print("the floor beneath you drops!")
        game_over()
    else:
        print ("you have entered an invalid option, please input '1' or '2'")
        level_1(name)
    
def level_2(name):
    print("welcome to level 2",name)
    time.sleep(2)
    print("you may be wondering why level 1 was so easy")
    time.sleep(2)
    print("from now on that wont be the case...")
    time.sleep(2)
    print("when you walked through the door from level 1")
    print("the door slammed shut behind you")
    time.sleep(3)
    print("you notice you are in another room with various items")
    time.sleep(2)
    print("you notice that the door has another 4-digit code lock")
    time.sleep(2)
    print("but you know that this time it is going to be differant")
    time.sleep(2)
    print("you start looking around the room")
    def level_2_choices(name):
        print("you have a few differant choices")
        time.sleep(2)
        print("1) try the combination lock")
        print("2) look at the picture in the corner")
        print("3) check the desk draws")
        choice = int(input())
        if choice == 1:
            print("you walk over to the door")
            print("**input combination**")
            combination = input()
            if combination == "1904":
                print("well done,",name,"!")
                level_3(name)
            else:
                print("wrong combination")
                level_2_choices(name)
        elif choice == 2:
            print("you walk over to the picture on the wall")
            time.sleep(2)
            print("you lift the picture off the wall and see a note in a hole in the wall")
            time.sleep(2)
            print("you pick it and it has the code 4567")
            level_2_choices(name)
        elif choice == 3:
            print("you walk over to the draws")
            time.sleep(2)
            print("it has a combination lock(4_digit)")
            time.sleep(2)
            print("**enter combination**")
            draw_combination = input()
            if draw_combination == "4567":
                print("you find a note with the combination 1904")
                level_2_choices(name)
            else:
                print("wrong combination")
                level_2_choices(name)
        else:
            print("please input a valid answer!!")
            level_2_choices(name)
    level_2_choices(name)

**def level_3(name):**
**    print("welcome to level 3",name,"!")**
**    time.sleep(2)**
**    print("the door slams shut yet again")**
**    time.sleep(2)**
**    print("you see a backpack on the floor")**
**    time.sleep(2)**
**    print("you look inside it.")**
**    time.sleep(2)**
**    print("it is empty")**
**    time.sleep(2)**
**    print("you put it on your back")**
**    def level_3_choice():**
**        inventory = []**
**        print("you look around the room and notice that there are loads of items in this room")**
**        print("you have various choices")**
**        print("1)Unlock the door(key lock)")**
**        print("2)Look at the picture on the wall")**
**        print("3)look at the carpet peeling up in the corner")**
**        print("4)Open the safe(word lock)")**
**        choice = int(input())**
**        if choice == 1:**
**            print("you walk over to the door")**
**            if "door_key" in inventory:**
**                print("you take the key out of your backpack and open the door")**
**                level_4(name)**
**            else:**
**                print("you need the door key to open the door!")**
**                level_3_choice()**
**        elif choice == 2:**
**            print("you walk over to the picture on the wall")**
**            if "screwdriver" in inventory:**
**                print("you open your backpack and get out the screwdriver")**
**                print("you take down the picture off the wall")**
**                print("there is a note that says the word 'password'")**
**                level_3_choice()**
**            else:**
**                print("it is screwed on you will need a screwdriver")**
**                level_3_choice()**
**        elif choice == 3:**
**            print("you walk over to the corner of the room and see the carpet peeling up")**
**            print("you pull it up and see a screwdriver on the bare floorboards")**
**            print("you pick it up and put it in your backpack")**
**            inventory.append("screwdriver")**
**            level_3_choice()**
**        elif choice == 4:**
**            print("you walk over to the safe")**
**            print("it requires a password")**
**            print("***please input password***")**
**            safe = input()**
**            if safe == "password":**
**                print("you open the safe and find a key")**
**                inventory.append("door_key")**
**                level_3_choice()**
**            else:**
**                print("wrong password")**
**                level_3_choice()**
**        else:**
**            print("please input a valid number!!")**
**            level_3_choice()**
**    level_3_choice()**        



game_intro()

the starred part is where the list is.
when i run code it is meant to add to the list (inventory) using the .append() function but it either does not add to the list or does not find it in the list later in the game.
please help :slight_smile:


#2

inventory has a global scope, it only exist within the level_3 function


#3

it starts within the level_3_choice function


#4

oops, i meant to say inventory has a local scope, it only exist within level_3_choice function.

calling leveL_3_choice again will cause the list become empty, given inventory = [] will make the list empty again


#5

so would I have to send it as a variable like I have done with the ‘name’ variable


#6

using parameters and arguments is a good option


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.