I'm trying to read the story of an adventure game from a file into a dictionary, then have the player dictate the advances through the game with either "next" or "back". The first function work's fine. And printing "print(room_dict[0])" will call the first room description.
def room(room_dict):
with open("worlds\\rooms.txt", "r") as room_file:
for line in room_file:
room_key = int(line.strip())
room_description = next(room_file).strip()
room_dict[room_key] = room_description
return room_dict
room_dict = {}
room = room(room_dict)
def room_interaction():
print(room_dict[0])
VALID = ("next", "back", "search")
current = (room_dict[0])
room_choice = input("What do you want to do?: ").lower()
while room_choice not in VALID:
room_choice = input("What do you want to do?: ").lower()
if room_choice == "next":
print(room_dict[current + 1])
current = (room_dict[current + 1])
elif room_choice == "back":
print(room_dict[current - 1])
current = (room_dict[current - 1])
The problem arises when I try to either add one or subtract one, I get the traceback:
File "C:\room interaction test.py", line 23, in room_interaction
print(room_dict[current + 1])
TypeError: Can't convert 'int' object to str implicitly
I know the +1/-1 method probably isn't the best, but it's the simplest I could come up with on short notice. Any other ideas on how to move throughout a dictionary in this manner?