-2

I have done most of the work but currently having problems with my dictionary linking rooms and then the items within together. Line 21 (which begins with 'Main Fair ground': {'South':) is too long and invalid. Below is the code I have done so far, I will be grateful for any help because I am new to Python and struggling a little bit.

# Sample function showing the goal of the game and move commands
def show_instructions():
    # print a main menu and the commands
    print("Killer Wolf Text Adventure Game")
    print("Collect 6 items to win the game, or be eaten by the killer Wolf.")
    print("Move commands: go South, go North, go East, go West")
    print("Add to Inventory: get 'item name'")


def player_stat():
    print("-" * 20)
    print('You are in the {}'.format(currentRoom))
    print("-" * 20)


def main():
    pass


rooms = {
    'Main Fair ground': {'South': 'Food stand', 'item' : '1LB of meat', 'North': 'Arcade', 'item' : 'real sword', 'East': 'Corn field', 'item' : 'wolf repellent', 'West': 'Candy shop', 'item' : 'candy'},
    'Security': {'item': 'protective gear', 'East': 'Food stand'},
    'Gift shop': {'item': 'map', 'West': 'Arcade'},
    'Petting area': {'item': 'killer wolf', 'South': 'Corn field'}  # villain
}

currentRoom = 'Main Fair ground'
player_move = ''

while currentRoom != 'Exit':
    player_stat()
    player_move = input('Enter your move:\n').lower()
    if player_move in ['Exit', 'exit']:
        currentRoom = 'Exit'
        print('Play again soon')
        continue
try:
    currentRoom = rooms[currentRoom][player_move]
except Exception:
    print("invalid move")
    continue

if 'Petting' == currentRoom:
    print("Kill the wolf")

The error message I get is:

File "wolf.py", line 41 continue ^ SyntaxError: 'continue' not properly in loop

Here is an example of a dictionary from a dragon text game, this is basically what I am doing so this is a great example.

#A dictionary linking a room to other rooms
#and linking one item for each room except the Start room (Great Hall) and the room containing the villain
rooms = {
   'Great Hall' : { 'South' : 'Bedroom', 'North': 'Dungeon', 'East' : 'Kitchen', 'West' : 'Library' },
   'Bedroom' : { 'North' : 'Great Hall', 'East' : 'Cellar', 'item' : 'Armor' },
   'Cellar' : { 'West' : 'Bedroom', 'item' : 'Helmet' },
   'Dining Room' : { 'South' : 'Kitchen', 'item' : 'Dragon' } #villain
}

The player should enter a command to either move between rooms or to get an item, if one exists, from a room. The gameplay loop should continue looping, allowing the player to move to different rooms and acquire items until the player has either won or lost the game. Remember that the player wins the game by retrieving all of the items before encountering the room with the villain. The player loses the game by moving to the room with the villain before collecting all of the items. Be sure to include output to the player for both possible scenarios: winning and losing the game.

Al Sweigart
  • 11,566
  • 10
  • 64
  • 92
  • 2
    What exactly is your question? – SuperStormer Apr 13 '21 at 00:36
  • I need help reformatting the dictionary for the rooms and the items. That python code is invalid, line 21 is way to long and I didn’t do the items right. Please please help me. – Ms.Python_newbie Apr 13 '21 at 00:40
  • There can only be 1 of each key in a python `dict`. So you are correct, you cannot have 4 values keyed to 'item' in your `rooms['Main Fair ground']` nested dictionary. You might consider having a separate `dict` of items setup with the same cardinal direction keys as you have rooms. – Henry Ecker Apr 13 '21 at 01:09

3 Answers3

0

I couldn't understand quite well what you want to do, but on the example dict it looks like each room with one item or more than one path in turns to be a key on the rooms dictionary with value being another dict with keys being either a direction or item and values as the name of the neighbor room or the name of the item. If it is so, I think the only problem is inside the "Main Fair Ground" dictionary, as I stated each inner dict must have at most one "item" key, to do that, you should put inside the "Main Fair Ground" only the directions and add new entries on the "rooms" dictionary to add the items, something like:

rooms = {
    'Main Fair ground': {'South': 'Food stand', 'North': 'Arcade', 'East': 'Corn field', 'West': 'Candy shop'},
    'Food stand': {'item': '1LB of meat', 'North': 'Main Fair ground'},
    'Arcade' : {'item': 'real sword', 'South': 'Main Fair ground'}
    ...
}

If what you wanted was that 1LB of meat is the item inside the Food Stand, and the real sword inside the arcade this should probably work.

Luís Otávio
  • 183
  • 1
  • 13
0

Dictionaries must have unique keys, but you've specified multiple "item" keys in the first room. You've also specified several other rooms within the main fair ground that have no definition elsewhere. E.g. you specify "South": "Food stand" but you don't have a "Food stand" room. You should pull those out into separate room dicts and move the item into that, e.g.:

rooms = {
    "Main fair ground": {"South": "Food Stand", "North": "Arcade", "East": "Corn field", "West": "Candy Shop"},
    "Food stand": {"item": "1LB of meat"},
    "Arcade": {"item": "real sword"},
    "Corn field": {"item": "wolf repellent"},
    "Candy shop": {"item": "Candy"},
    'Security': {'item': 'protective gear', 'East': 'Food stand'},
    'Gift shop': {'item': 'map', 'West': 'Arcade'},
    'Petting area': {'item': 'killer wolf', 'South': 'Corn field'}
}

Then presumably you want to add some possible cardinal directions for those new rooms so someone can move between them and somewhere else. You'll need to figure out what your map looks like there.

Also, you have a continue outside of your while loop:

try:
    currentRoom = rooms[currentRoom][player_move]
except Exception:
    print("invalid move")
    continue

This block isn't indented to be within the while loop. Neither is line 44 and 45. You're going to end up permanently stuck in that while loop without hitting line 39, until you type "exit", at which point you'll hit line 39 exactly one time before your script raises a SyntaxError: "continue" not properly in loop.

You need it indented like:

while currentRoom != 'Exit':
    player_stat()
    player_move = input('Enter your move:\n').lower()
    if player_move in ['Exit', 'exit']:
        currentRoom = 'Exit'
        print('Play again soon')
        continue
    try:
        currentRoom = rooms[currentRoom][player_move]
    except Exception:
        print("invalid move")
        continue

    if 'Petting' == currentRoom:
        print("Kill the wolf")

You should also reconsider catching that overly broad exception. It will work in this case, but it's bad practice. You should establish what exception will actually be raised if a user types in an invalid move and catch exactly that exception. In this case, it would be a KeyError if the player's move doesn't match up to a defined direction.

Making overly broad exceptions can hide other bugs in your code. There is a time and place to catch Exception, for example, if you're writing a library and need to catch all exceptions and then re-raise a custom exception with additional context, but this is probably not that situation.

dephekt
  • 446
  • 2
  • 9
0

Your code has a continue statement that isn't in a loop. I think you need to indent the try: line and the lines after that so they're considered inside the while loop above them. Otherwise, Python considers these lines to be after the loop instead of inside it.

Al Sweigart
  • 11,566
  • 10
  • 64
  • 92