0

I am writing this for a class. I keep running issues with being able to validate if a person is typing 'Go' or 'Get'. If i type 'bo north', the code will execute to the next room. It should give an error code. I also think my def move_directions can be shortened but I am having trouble with anything to work. I tried adding directions as a list and use that, but that didn't work. I was trying to get player input to some how match with the direction and then move rooms by rooms[current_rooms][direction].

Very new to this and not fully understanding the best practices yet. Such as what the name in main func does and if I could use it???

Any help is appreciated.

    # Sample function showing the goal of the game and move commands
    def show_instructions():
        # print a main menu and the commands
        print("Santa's Great Escape Adventure Game")
        print("Collect 6 items to win the game, or get caught by the family dog, Snarls Bite-ley.")
        print("Move commands: go South, go North, go East, go West")
        print("Add to Inventory: get 'item name'")
    
    
    def player_stat(current_room, inventory, rooms):
        print('--------------------------------------------------------')
        print('You are in the {}.'.format(current_room))
        print('Inventory:', inventory)
        i = len(inventory)
        inventory_count = 6 - int(i)
        print('Items Left: {} more'.format(inventory_count))
        if current_room != 'Living Room':
          print('You see the', rooms[current_room]['item'], end='.\n')
        print('--------------------------------------------------------')
    
        # A dictionary linking a room to other rooms
        # and linking one item for each room except the Start room (Living) and the room containing the villain
    
    
    def main():
        rooms = {
            'Living Room': {'South': "Kids' Bedroom", 'North': 'Office', 'East': 'Dining Room', 'West': 'Master Suite'},
            'Master Suite': {'East': 'Living Room', 'item': 'Christmas Bell'},
            "Kids' Bedroom": {'East': 'Playroom', 'North': 'Living Room', 'item': 'Magic Dust'},
            'Playroom': {'West': "Kids' Bedroom", 'item': 'Dog Bone'},
            'Dining Room': {'West': 'Living Room', 'North': 'Kitchen', 'item': 'Cookies'},
            'Kitchen': {'South': 'Dining Room', 'item': 'Elf On The Shelf'},
            'Office': {'South': 'Living Room', 'East': 'Bonus Room', 'item': 'Dog Blankie'},
            'Bonus Room': {'West': 'Office', 'item': 'Snarls Bite-ley'}  # Villain
        }
    
        current_room = 'Living Room'
        player_move = ''
        inventory = []
        show_instructions()
    
        def move_directions(player_move, current_room):
    
            if player_move == 'South':
                return rooms[current_room]['South']
            elif player_move == 'North':
                return rooms[current_room]['North']
            elif player_move == 'East':
                return rooms[current_room]['East']
            elif player_move == 'West':
                return rooms[current_room]['West']
    
        def get_item():
            if player_move in rooms[current_room]['item']:
                inventory.append(rooms[current_room]['item'])
    
        while True:
    
            while len(inventory) <= 6 and current_room != 'Bonus Room':
    
                player_stat(current_room, inventory, rooms)
    
                if len(inventory) == 6 and current_room == 'Bonus Room':
                    print("You give Snarls Bite-ly his bone and blanket and escape the out the Bonus Room window.")
                    print("YOU WIN!")
                    print("MERRY CHRISTMAS!")
    
                user_input = input('Enter your move:\n').title().split()
                print(user_input[0])
    
               
                
                if user_input[0] == 'Go' or 'Get':
                  user_input.pop(0)
                  player_move = ' '.join([str(elem) for elem in user_input])
                print(player_move)  # used for testing of split REMOVE LATER
    
                if player_move == 'Exit':
                    exit('You have exited the game. Play again soon!')
                     # Prints when game exited, ends game
    
                # Game play loop to validate moves.
                elif player_move in rooms[current_room]:
                    current_room = move_directions(player_move, current_room)
                # need to validate items and add to inventory if not in inventory
                elif player_move in rooms[current_room]['item']:
    
                    if player_move in inventory:
                        print('**You already have this item. Proceed to another room.**')
    
                    elif player_move != inventory:
                        get_item()
    
                elif user_input[0] != 'Go' or 'Get':
                  print("That is not a valid move. Try again.")
                
                else:
                    print("That is not a valid move. Try again.")  # Display if invalid move
    
            quit('******** Snarls Bite-ley AWAKES! ********\n'
                 'You are Trapped and the Family AWAKES!\n'
                 '************ G A M E O V E R ************')
            break
    
    
    main()

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • 2
    First, this: `if user_input[0] == 'Go' or 'Get':` is interpreted as `if (user_input[0] == 'Go') or 'Get':`, and since 'Get' is always True, this `if` is always taken. You want `if user_input[0] in ('Go','Get'):`. Once you do that, you need to think about what happens if that DOESN'T match, because you aren't handling that right now. – Tim Roberts Dec 11 '21 at 03:13
  • or `if user_input[0] not in ('Go','Get'):` – meowulf Dec 11 '21 at 03:19
  • Thanks! That definitely makes more sense and I can see where I wasn't formatting the input correctly. I guess I can make a list of the moves, match the user input to the lists and then split the the input to move forward. I thought I was simplifying my trying to bounce everything off the dictionary, but i guess additionally lists may be easier. Not sure if anyone will read this, but my professor made a comment on my last project referring to if __name__in __main ???? Zybooks might have had a small section about it, but not sure how to use that? Where would that be useful? – Lorie Zimmerman Dec 11 '21 at 18:44

0 Answers0