I'm attemting to learn programming with Python and am currently trying to design an adventure game that sets up rooms
and a player can move between them. I'm having problems working out a lop function that can move the player around the rooms.
The current method has an error code saying 'list index out of range'. I don't want to throw in the towel and will carry on trying to
debug the programme but is there a better way to loop the method and is there a way to show a message if a wrong direction
is chosen. The code is shown below and any help or advice is much appreciatted.


#Adventure rooms - a programme to set up a room move system 
#Set up a room class

class room:
def __init__ (self,description,exits,connecting_rooms):
    self.description = description
    self.exits = exits
    self.connecting_rooms = connecting_rooms
#set up a player class
class player:
def __init__ (self,name,location):
    self.name = name
    self.location = location
# set up two rooms

room1 = room("a coridoor, with exits south",["s","e","n"],["room2"])
room2 = room("the middle of the coridoor, with exits north and east", ["n","e"],["room1","room3"])
room3 = room("an office, with a desk and stuff",["w"],["room2"])

#set up a player

player1 = player("tharg the destroyer",room1)

# set up a look function

def look():
#function that displays room information
message = "you are in " + player1.location.description
print (message)

# set up a move function that changes the room 
def move(room):
if room == "room1":
    player1.location = room1
elif room == "room2":
    player1.location = room2
elif room == "room3":
    player1.location = room3

    print ("You cannot move this way")
# set up a function that takes a direction and moves to the coresponding room
 #check that the currect room has the direction given

def roomChange(direction):     

 #takes a direction, checks it is valid then uses move function to change rooms
 exit_count = len(player1.location.exits)
 print (exit_count)

 for i in range (exit_count) :

     if direction == player1.location.exits[i]:


#test the code

#player goes to south room
#this room change shows the index out of range error message

Room1 has three exits but only one connecting room, so any attempt to go in the second or third exit will crash for lack of a corresponding connecting room.

commented: Hi, thanks for the reply. I put the extra exits in to test some of the code and forgot to delete it. +0

If you are going to define a class for a player then you should define a changeroom or move method within that class instead of making it an external def.

I also want to mention that you should post code with the correct indentation (especially important for python code).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.