0

My code works fine moving between rooms and collecting items but when player gets to the Food court where the villain is without all 6 items in the inventory it should output the player lost, or if player has all item then player wins. See my code below, I added an "if len(Inventory) == and !=" but it isn't working. Any help is appreciated. This is my first programing/scripting course I take so I need this code probably needs a lot more work than what I think but this is what I have so far.

   #enter code here#
#instructions
print('You are at the mall and there is an Alien in the Food Court that you must defeat!')
print("You can only move around the mall by typing 'North', 'South', 'East', or 'West' when asked for your move. Type 'Exit' at any time to leave the game. ")
print("You need to collect 6 items from 6 different stores to defeat the Alien.")
print("If you get to the Alien without all 6 items in your inventory, you die and lose the game.")
print('Good luck!')

rooms = { 'Marshalls': {'name': 'Marshalls', 'East': 'Miscellaneous', 'North': 'Champs'},
    'Miscellaneous': {'name': 'Miscellaneous', 'West': 'Marshalls'},
    'Champs': {'name': 'Champs', 'North': 'Pharmacy', 'South': 'Marshalls', 'East': 'Sports', 'West': 'Food Court'},
    'Sports': {'name': 'Sports', 'North': 'Verizon'},
    'Verizon': {'name': 'Verizon', 'South': 'Sports'},
    'Food Court': {'name': 'Food Court', 'East': 'Champs'},
    'Pharmacy': {'name': 'Pharmacy', 'East': 'Toys', 'South': 'Champs'},
    'Toys': {'name': 'Toys', 'West': 'Pharmacy'},
}

items = {'Miscellaneous': 'Rope',
         'Champs': 'Running shoes',
         'Sports': 'Baseball bat',
         'Verizon': 'Phone',
         'Pharmacy': 'Tranquilizer',
         'Toys': 'Water gun'
}

direction = ['North', 'South', 'East', 'West']      #how player can move
current_room = 'Marshalls'
Inventory = []

#function
def get_new_statement(current_room, direction):
    new_statement = current_room  #declaring current player location
    for i in rooms:  #loop
        if i == current_room:
            if direction in rooms[i]:
                new_statement = rooms[i][direction]  #assigning new direction to move player

    return new_statement  #return

while True:
    print('You are in the', current_room, 'store')
    print('Current inventory:', Inventory)
    print('__________________________')
    direction = input('Type an allowed direction to move to another store? or type Exit to leave game: ' )
    direction = direction
    if (direction == 'exit'):
        print('Goodbye, thanks for playing!')
        exit()
    if (direction == direction):
        new_statement = get_new_statement(current_room, direction)
        if new_statement == current_room:
            print('Move not allowed, try again.')
        else:
            current_room = new_statement
    else:
        print('Invalid entry')

    #ask to collect item in current room
    if current_room in items.keys() and items[current_room] != None and items[current_room] != 'Food Court':
        print('You an item: ', items[current_room])
        option = input('Do you wish to collect it (y/n): ')
        if option[0] == 'y' or option == 'Y':
            Inventory.append(items[current_room])
            items[current_room] = None

    #if player gets to the Food Court then player either win or looses the game
    if current_room in items.keys() and items[current_room] == 'Alien':
        if len(Inventory) == 6:
            print('Congrats you defeated the Alien!!')
        elif len(Inventory) != 6:
            print("You lost")
            print('Goodbye')
        break



  
LizzzPyyy
  • 3
  • 2

1 Answers1

0

You need to change the line

if current_room in items.keys() and items[current_room] == 'Alien':

to

if current_room == 'Food Court':

Because current_room isn't in items, so the if statement always goes false. If you put Food Court in items, with alien as its value, then it will ask you if you want to collect it. So you need to just check if the room is Food Court, then it will evaluate to true.

Note: You might want to make it check if direction.lower() is in the room's values, so the user can enter any type of capitalized word and it will move if it can.

KaliMachine
  • 191
  • 9
  • If you have any questions about it, just comment and I'll try to help. – KaliMachine Oct 12 '22 at 23:58
  • KaliMachine, thank you so much. I followed your input and it worked! thanks again for the advices. – LizzzPyyy Oct 13 '22 at 03:31
  • Keep coding and have fun! – KaliMachine Oct 13 '22 at 12:53
  • you also might want to add 'West': 'Champs' to Sports, so you can get away from sports and verizon. – KaliMachine Oct 13 '22 at 12:56
  • Otherwise when you get there you will be stuck. – KaliMachine Oct 13 '22 at 12:56
  • oh wow thank you!!!, for as long as I've been looking at this code I can't believe I had overlook this detail completely. – LizzzPyyy Oct 13 '22 at 22:54
  • I know my code isn't the most organized but I'm very beginner at this so I'm hoping my professor doesn't take too many points away, and to get this code to work I couldn't figure out how to make "item" a key so I created a second dictionary {} for items. Do you know any easier way to keep one dictionary altogether that has the rooms and items? – LizzzPyyy Oct 13 '22 at 22:57
  • I think that two dictionaries is the way to go, although if you really want to, you can just put both dictionaries in the same one, like ```mainDict = {dict1:{}, dict2:{}}``` – KaliMachine Oct 14 '22 at 22:03