2

Hi I am very new to Python and I'm in the process of learning about lists.

Im writing a little puzzle text game that requires you to use basic commands to move from room to room (function to function). Some rooms have traps and they require an item to remove the trap. E.G A bear in a room requires you to first find the honey and then give it to the bear. I also understand how to .remove() and .append() the list.

I've created 2 lists:

inventory = ["Honey"]
trap = ["Bear"]

When I enter the room with the bear and I have the honey, awesome I can pass, but how do I make a loop or #Something# that checks the 'trap' list so that if the "bear" element isn't in the list they don't require the honey to pass through as obviously i remove the "honey" from the 'inventory' list as soon as they use it on the bear. The theory is that if they exit the room and want to re-enter they don't require the honey again.

I guess my understanding of this is something like this, also assume you already found the honey:

def room1()
    print "You are in ROOM 1"

    if #item is not in list# and #bear is not in the room#:
        room2()
    elif #item is in the bag#:
        print "You give the honey to the bear, and it is distracted"
        inventory.remove("Honey")
        trap.remove("Bear")
        room2()
    else:
        print "You die to the bear"
        exit()

I would really appreciate any advice or even a different approach to this issue. Thanks so much!

Congi
  • 23
  • 3

4 Answers4

1

Use the in membership operator to determine if an element is in a list. This requires no loop it will test each element.

if 'honey' in inventory and 'bear' not in trap:
    room2()
elif ...
Paul Joireman
  • 2,689
  • 5
  • 25
  • 33
0

I couldn't get the full picture, but i hope you are looking to check if an item is present in a list. you can simply use "if & in"

if "bear" in trap: #use as required in your scenario
    #do the required
Headrun
  • 129
  • 1
  • 11
0

Maybe my answer is out of topic cause you wanted to learn just lists and you talk about rooms as function to function, here I made a simple game using a bit more complex lists (I hope that helps you in the understanding of lists) gathering information about neightboors rooms, and traps.

class puzzle:
def __init__(self):
    self.lives = 1
    self.inventory = ['honey']
    self.trap_object = {'bear': 'honey', 'fire': 'water'}
    self.rooms = [
        ['room 0', {'trap': None, 'doors': [1,2,3]}],
        ['room 1', {'trap': 'bear', 'doors': [0,3]}],
        ['room 2', {'trap': 'fire', 'doors': [0,4]}],
        ['room 3', {'trap': None, 'doors': [0,1,5]}],
        ['room 4', {'trap': None, 'doors': [2,6]}],
        ['room 5', {'trap': 'bear', 'doors': [3]}],
        ['room 6', {'trap': None, 'doors': [4]}]
    ]

def gameLoop(self):
    actual_room = self.rooms[0]
    while(self.lives > 0):
        print "You are in room %s.\n" % (actual_room[0])
        n_doors = len(actual_room[1]['doors'])
        print "You have %d doors:.\n" % (n_doors)
        for n_door in actual_room[1]['doors']:
            print "Room number %d\n" % (n_door)

        valid = False
        room = None
        while not valid:
            room  = int(raw_input('Select a room:\n'))
            if room in actual_room[1]['doors']:
                valid = True

        print "You enter in room %d..." % (room)
        actual_room = self.rooms[room]

        if actual_room[1]['trap'] == None:
            print "The room seems to be safe!\n"
        else:
            trap = actual_room[1]['trap']
            useful_obj = self.trap_object[trap]
            print "There is a trap in this room!: %s\n" % (trap)
            if useful_obj in self.inventory:
                print "You had an object to avoid the trap :)\n"
                self.inventory.remove(useful_obj)
            else:
                print "You had nothing to avoid the trap :(\n"
                self.lives -= 1



game = puzzle()
game.gameLoop()
Iris G.
  • 433
  • 1
  • 6
  • 15
  • Wow! That looks pretty awesome! Thanks heaps for going through all that. I don't understand half of it though haha, I'm only now moving onto classes. There are also a fair few things I don't understand in the function. But that's not your fault that's just my lack of understanding atm sorry. I'm brand new. Thanks again! – Congi May 26 '15 at 12:03
  • I think I will haha. Is there a program that you can use to step through code? – Congi May 26 '15 at 12:27
0

You already did the difficult part, just translate your english in Python:

if #item is not in list# and #bear is not in the room#:

becomes:

if 'Honey' not in inventory and 'Bear' not in trap:

Furthermore, I'd check if the bear is present first, and only then if I have Honey:

def room1()
    print "You are in ROOM 1"
    if 'Bear' not in trap:
        room2()
    else:
        if 'Honey' in inventory:
            print "You give the honey to the bear, and it is distracted"
            inventory.remove("Honey")
            trap.remove("Bear")
            room2()
        else:
            print "You die to the bear"
            exit()
Hrabal
  • 2,403
  • 2
  • 20
  • 30