简体   繁体   中英

While Loop for Adventure Game IN Python

I am following the instructions from this lab: http://programarcadegames.com/index.php?chapter=lab_adventure&lang=en

I am confused on how to allow the user to proceed to other rooms after setting up these if statements. I followed the instructions but I might be incorrect. Here is my code so far:

room_list=[]

room = ["You are in a small bedroom! There is a door to the north.", 1, None, None, None]
room_list.append(room)
room = ["You are now in a bigger bedroom! There is a door to the East!", None, 2, None, None]
room_list.append(room)
room = [ "You are now in the North hall! There is a Door to the South!", None, None, 3, None]
room_list.append(room)
room = [" You are now in the South hall! There is a door to the East!", None, 4, None, None]
room_list.append(room)
room = [" You are now in the dining room but you smell something past the North door!", 5, None, None, None]
room_list.append(room)

current_room = 0


done = False
while not done:
   print(room_list[current_room][0])
   user_c = input("What direction do you want to go?")
   if user_c.lower() == "north":
      next_room = room_list[current_room][1]
      current_room = next_room
   elif user_c.lower() == "east":
      user_c = room_list[current_room][2]
   elif user_c.lower() == "south":
      user_c = room_list[current_room][3]
   elif user_c.lower() == "west":
      user_c = room_list[current_room][4]
   else:
      print("I dont understand.")
   if user_c == None :
      print("You can't go that way.")

Once the user has gone to the North room how do I proceed to the East room without conflicting with the other if statements?.. Apologize for the dumb question and thank you for your help!

You might want to try using really simple classes and dictionaries. They're essentially what you're looking for.

class Room:
    def __init__(self, text, other_rooms=None):
        self.text = text
        self.other_rooms = other_rooms

    def set_other_rooms(self, other_rooms):
        self.other_rooms = other_rooms

rooms = [Room("You are in a small bedroom! There is a door to the north."),
         Room("You are now in a bigger bedroom! There is a door to the East!"),
         Room("You are now in the North hall! There is a Door to the South!"),
         Room("You are now in the South hall! There is a door to the East!"),
         Room("You are now in the dining room. You smell something past the North door!"),]

rooms[0].set_other_rooms( { "north":rooms[1],
                            "south":None,
                            "east":None,
                            "west":None } )

rooms[1].set_other_rooms( { "north":None,
                            "south":None,
                            "east":rooms[2],
                            "west":None } )

rooms[2].set_other_rooms( { "north":None,
                            "south":rooms[3],
                            "east":None,
                            "west":None } )

rooms[3].set_other_rooms( { "north":None,
                            "south":None,
                            "east":rooms[4],
                            "west":None } )

rooms[4].set_other_rooms( { "north":rooms[5],
                            "south":None,
                            "east":None,
                            "west":None } )

current_room = None
done = False

while not done:
    print(current_room.text)
    user_c = input("What direction do you want to go?").lower()
    if user_c == "exit":
        done = True
    elif current_room.other_rooms[ user_c ]:
        current_room = current_room.other_rooms[ user_c ]
    else:
        print("You can't go that way.")

Also, man, that is a really awful tutorial. It might have some merit for teaching beginners Python, but it's not at all what you'd want to do if you were actually developing in Python. While there really is a lot of great content on the web to learn Python, there's also a lot of garbage. Acknowledge that there is no best way to do things, but simultaneously try to follow best practices as much as possible. That usually consists of consulting with the Stack Exchange.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM