I'm making a text adventure game based on rooms. If you run the code, it works if you go within the room boundaries (ie start at bedroom 2 and go to bedroom 1 vice versa), however as soon as you go out of the boundaries, it gives an error:
>>> print(room_list[int(current_room)][0])
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
Upon deleting the int()
, I get a different error:
TypeError: list indices must be integers or slices, not str
How do I fix it to retain the None
type? Or just make the code work. Below is my code.
def main():
done = False
room_list = []
current_room = 0
# Append rooms to different numbers
room = ["\nYou are in bedroom 2.\nThere is a passage north and east.", "3", "1", None, None] # Room name, N, E S, W
room_list.append(room)
room = ["\nYou are in at southern hall.\nThere is a passage north and east.", "4", "2", None, "0"]
room_list.append(room)
room = ["\nYou are in the dining room.\nThere is a passage north and west.", "5", None, None, "1"]
room_list.append(room)
room = ["\nYou are in bedroom 1.\nThere is a passage east and south.", None, "4", "0", None]
room_list.append(room)
while not done:
print(room_list[int(current_room)][0])
answer = input('What direction? ')
# Check for correct direction and check if exists
if answer == "n":
next_room = room_list[int(current_room)][1]
current_room = next_room
if next_room == None:
print("You can't go that way. ")
elif answer == "e":
next_room = room_list[int(current_room)][2]
current_room = next_room
if next_room == None:
print("You can't go that way. ")
elif answer == "s":
next_room = room_list[int(current_room)][3]
current_room = next_room
if next_room == None:
print("You can't go that way. ")
elif answer == "w":
next_room = room_list[int(current_room)][4]
current_room = next_room
if next_room == None:
print("You can't go that way. ")
elif answer == "q":
done = True
else:
print("Invalid destination, try again.")
main()
Well, because current_room
(or next_room
) is None
... You should move current_room = next_room
after the if
statement, under an else
clause
def main():
done = False
room_list = []
current_room = 0
# Append rooms to different numbers
room = ["\nYou are in bedroom 2.\nThere is a passage north and east.", "3", "1", None, None] # Room name, N, E S, W
room_list.append(room)
room = ["\nYou are in at southern hall.\nThere is a passage north and east.", "4", "2", None, "0"]
room_list.append(room)
room = ["\nYou are in the dining room.\nThere is a passage north and west.", "5", None, None, "1"]
room_list.append(room)
room = ["\nYou are in bedroom 1.\nThere is a passage east and south.", None, "4", "0", None]
room_list.append(room)
while not done:
print(room_list[int(current_room)][0])
answer = input('What direction? ')
# Check for correct direction and check if exists
if answer == "n":
next_room = room_list[int(current_room)][1]
if next_room is None: # == None can be replaced with is None
print("You can't go that way. ")
else:
current_room = next_room
elif answer == "e":
next_room = room_list[int(current_room)][2]
if next_room is None:
print("You can't go that way. ")
else:
current_room = next_room
elif answer == "s":
next_room = room_list[int(current_room)][3]
if next_room is None:
print("You can't go that way. ")
else:
current_room = next_room
elif answer == "w":
next_room = room_list[int(current_room)][4]
if next_room is None:
print("You can't go that way. ")
else:
current_room = next_room
elif answer == "q":
done = True
else:
print("Invalid destination, try again.")
main()
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.