I am taking a coding class and doing the ever so famous text based game, I have tried various ways to try and get this game to work and no variation will work When it comes to entering the command to change rooms; it says invalid commands. So i cannot test anything further until I am able to get that far. I had it working and then I added a more complex dictionary and everything went to poop. I dont know if i need to go back down to simpler dictionaries with easier identifyers or if i can simply find a way to make this work.
this is the full code:
areas_dic = {
Home : { name : Home , go East : Plains , item : none },
Plains : { name : Plains , go North : Rocky Terrain , go East : Fields , go South : Stream , item : Grains },
Stream : { name : Stream , go East : Forest , item : Water },
Forest : { name : Forest , go West : Stream , item : Firewood },
Fields : { name : Fields , go West : Plains , go North : Farmlands , item : Vegetables },
Rocky Terrain : { name : Rocky Terrain , go South : Plains , go East : up the Mountain , item : Medicinal Flowers },
up the Mountain : { name : up the Mountain , go West : Rocky Terrain , go East : Mountain Top , item : Warmer Clothes },
Farmlands : { name : Farmlands , go South : Fields , go West : DYSENTERY , item : Jerky },
Mountain Top : { name : Mountain Top , go West : up the Mountain , go South : DYSENTERY , item : Milk }
}
print( Welcome to Old Americas )
print( Object of the game is to collect all 7 items before falling ill )
print( You can type go North, go South, go East, go West to move )
current_location = Home
inventory = []
required_items = { Grains , Water , Firewood , Vegetables , Medicinal Flowers , Warmer Clothes , Milk }
while True:
print(f"You are in the {areas_dic[current_location][ name ]}.")
# Display available directions
directions = [direction for direction in areas_dic[current_location] if direction.startswith( go )]
print("Available directions:", ", ".join(directions))
# Get player input
command = input("Enter your command): ").strip().lower()
# Process player input
> if command in directions:
> direction = command.split()[1]
> if direction in areas_dic[current_location]:
> current_location = areas_dic[current_location][direction]
item = areas_dic[current_location].get( item , None)
if item:
print(f"You found {item}!")
inventory.append(item)
if item in required_items:
required_items.remove(item)
print(f"Items left to collect: { , .join(required_items)}")
else:
print("You can t go that way!")
else:
print("Invalid command. Try again.")
# Check if player has collected all items
if not required_items:
print("Congratulations! You ve collected all items. Now face the villain in DYSENTERY!")
break
# Check if player has reached the final room
if current_location == DYSENTERY :
if illness in inventory:
print("You have fallen ill. Game over!")
else:
print("You defeated the illness and survived the journey! Well done!")
break
the block quote is where i am having the problems. or i think i am, i dont know if i am not formatting it correctly or if it needs something all together different.