Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1585

Python • Text Adventure

$
0
0
I am trying the text adventure and I have got to Page 116 in the The Official Raspberry PI Beginner's Guide 5th Edition.

I have added the monster and now I get an error Traceback (most recent call last):
File "/home/raspberry/Downloads/text-adventure.py", line 94
if 'item' in rooms[currentRoom]
^
SyntaxError: expected ':'


Below is the code

Code:

#!/bin/python3# A simple text adventure you can enhance with your own codedef showInstructions():    # print a main menu and the commands    print("""Text Adventure==============Commands:  go [direction]  get [item]""")def showStatus():    # print the player's current status    print("---------------------------")    print("You are in the " + currentRoom)    # show the current inventory    print("Inventory : " + str(inventory))    # show an item if there is one    if "item" in rooms[currentRoom]:        print("You see a " + rooms[currentRoom]['item'])    print("---------------------------")# an inventory, which is initially emptyinventory = []# a dictionary linking a room to other roomsrooms = {    'Hall' : {        'south' : 'Kitchen',        'east'  : 'Dining Room',        'item' : 'key'    },    'Kitchen' : {        'north' : 'Hall',        'item'   : 'monster'    },    'Dining Room' : {        'west' : 'Hall'    }}# start the player in the HallcurrentRoom = 'Hall'showInstructions()# loop foreverwhile True:    showStatus()    # get the player's next 'move'    # .split() breaks it up into an list array    # eg typing 'go east' would give the list:    # ['go','east']    move = ''    while move == '':        move = input('>')    move = move.lower().split()    # if they type 'go' first    if move[0] == 'go':        # check that they are allowed wherever they want to go        if move[1] in rooms[currentRoom]:            # set the current room to the new room            currentRoom = rooms[currentRoom][move[1]]        # there is no door (link) to the new room        else:            print("You can't go that way!")    # if they type 'get' first    if move[0] == 'get':        # if the room contains an item, and the item is the one they want to get        if 'item' in rooms[currentRoom] and move[1] in rooms[currentRoom]['item']:            # add the item to their inventory            inventory += [move[1]]            # display a helpful message            print(move[1] + " got!")            # delete the item from the room            del rooms[currentRoom]['item']        # otherwise, if the item isn't there to get        else:            # tell them they can't get it            print("Can't get " + move[1] + "!")# player loses if they enter a room with a monsterif 'item' in rooms[currentRoom]       and 'monster' in rooms[currentRoom]['item']:    print('A monster has got you... GAME OVER!')    break

Statistics: Posted by jjonesjths — Thu Oct 23, 2025 2:27 pm



Viewing all articles
Browse latest Browse all 1585

Trending Articles