Я делаю текстовую приключенческую игру, основанную на комнатах. Если вы запустите код, он сработает, если вы go в пределах границ комнаты (т.е. начнете со спальни 2 и go до спальни 1 и наоборот), однако, как только вы go выйдете за границы, это даст ошибка:
>>> print(room_list[int(current_room)][0])
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
После удаления int()
я получаю другую ошибку:
TypeError: list indices must be integers or slices, not str
Как исправить это, чтобы сохранить тип None
? Или просто заставить код работать. Ниже мой код.
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()