Reputation: 3
Added the map to my game in case that might be helpful in some way? I've tried my best to figure this out on my own using my textbook and the very helpful, useful answers already supplied here. However, it feels as though every attempt to fix my code only makes it worse. I'm certain my loop is causing the issue, but I can't figure out where I went wrong.
It prints everything correctly and asks for the input but, when I type in the input, it only calls the first loop. I thought it might be an indention problem, but PyCharm throws a fit when I indent the second loop. Thank you in advance.
**Updated "quit" or "QUIT" Thanks again, jarmod! ***Updated with latest code, thanks trincot!
Screenshot of output to show current issue:Input is needed twice before updating room and inventory? I see the formatting issue with the inventory. I think I can fix that... Maybe.
Here is my code:
spaces = {
"the outdoors": {"UP": "Barn", "RIGHT": "Magistrate's Home", "DOWN": "Candy Store"},
"Flowerhaus": {"RIGHT", "Barn"},
"Barn": {"LEFT": "Flowerhaus", "RIGHT": "Random Cabin", "DOWN": "the outdoors"},
"Magistrate's Home": {"UP": "Random Cabin", "DOWN": "Workshop", "LEFT": "the outdoors"},
"Workshop": {"UP": "Magistrate's Home", "LEFT": "Candy Store"},
"Candy Store": {"UP": "the outside", "LEFT": "Tavern", "RIGHT": "Workshop", "DOWN": "Papermill"},
"Tavern": {"RIGHT": "Candy Store", "DOWN": "Empty House"},
"Empty House": {"UP": "Tavern", "RIGHT": "Papermill"},
"Papermill": {"UP": "Candy Store", "LEFT": "Empty House"},
"Random Cabin": {"DOWN": "Magistrate's Home", "LEFT": "Barn"},
} # Dictionary for rooms and directions
items = {"Flowerhaus": None, "Tavern": None, "Barn": "Car Key", "Random Cabin": "Neighbor's Key",
"Magistrate's Home": "Deadbolt Key", "Workshop": "Office Key", "Empty House": "House Key",
"Papermill": "Mailbox Key", "Candy Store": "Ex's Key"} # Rooms as keys and key items as values.
current_space = "the outdoors" # Player starts outside
current_inventory = "a whole lot of nothing"
avail_directions = "UP", "DOWN", "RIGHT"
print("It's been a long, long, frustrating day.") # Print the story
print("You've entered the front door to your home, ready to take your shoes off and relax...")
print("As you walk through the door, you realize this is not your house, your city, or even your realm of existence.")
print("Warily, you step forward to further take in your surroundings.")
print("The floor is dirt, the buildings around you look strange, and the people don't seem to react to your presence.")
print("")
print("After a short time, you realize your keys are no longer in your hand.")
print("As you turn back around, you notice the door is still there, but now it has seven locks, each appearing")
print("to match the locks that go to all seven of your keys.")
print("Turning the knob proves the portal home is locked.")
print("Find all seven keys to unlock the door. Fail to find all seven keys and the door will disappear,")
print("leaving you trapped forever.")
print("")
print("So, with all of that in mind, where would you like to look first?") # End of printed story
direction = input("Type UP, DOWN, LEFT, or RIGHT").upper()
while direction != exit:
while direction in avail_directions:
print("You are currently in", current_space, "with", current_inventory, "on your keyring.") # Current status
direction = input("Type UP, DOWN, LEFT, or RIGHT").upper()
if direction in avail_directions:
current_space = spaces[current_space][direction]
avail_directions = tuple(spaces[current_space])
if current_space in items.keys():
if current_inventory == "a whole lot of nothing":
current_inventory = items[current_space]
print("You walk through the door and enter", current_space, end=".")
print("You found the", current_inventory, ".".strip())
else:
current_inventory += items[current_space]
print("You walk through the door and enter", current_space, end=".")
print("You found the", current_inventory, end=".")
elif direction == "QUIT":
print("Oh... So, you're going to take a break then? Yeah, maybe a nap is a good idea. Try again later.")
break
else:
print("Nothing over here. Don't you want to get back home? Try a different direction.")
continue
else:
print("Nothing over here. Don't you want to get back home? Try a different direction.")
direction = input("Type UP, DOWN, LEFT, or RIGHT").upper()
Upvotes: 0
Views: 1307
Reputation: 351403
Here is a list of issues that remain after some issues mentioned in comments were resolved in your later edit:
"Flowerhaus": {"RIGHT", "Barn"}
creates a set. The comma should be a colon.spaces
has a space called "the outdoors", but elsewhere it is called "the outside".current_inventory
gets its items glued to eachoter with the +=
assignment.current_inventory
string. It would be better to make the inventory a list to which you append items if they are not yet in it. The size of the list will then tell you whether all 7 keys were collected.exit
is a name that was never defined.else
to the while
statement is asking for input, but this input will never be processed, as the loop has already finished. I suggest to reserve this else
clause for telling the user they completed the adventure successfully (once the while
condition has been changed accordingly).input
is executed before the loop, during the loop and in the else
block. This should be rewritten to only have this input
happening at one place in your code.Some other things that could be improved:
avail_directions
directions should not be initialised like it is, but be controled by what the spaces
dictionary has for the current space. You don't even need this variable as you can just do if direction in spaces[current_space]
if..else
has code repetition. Just move the statements that are common to both out of this construct.Here is a proposed solution:
spaces = {
"outdoors": {"UP": "Barn", "RIGHT": "Magistrate's Home", "DOWN": "Candy Store"},
"Flowerhaus": {"RIGHT": "Barn"}, # typo
"Barn": {"LEFT": "Flowerhaus", "RIGHT": "Random Cabin", "DOWN": "outdoors"},
"Random Cabin": {"LEFT": "Barn", "DOWN": "Magistrate's Home"},
"Magistrate's Home": {"UP": "Random Cabin", "DOWN": "Workshop", "LEFT": "outdoors"},
"Workshop": {"UP": "Magistrate's Home", "LEFT": "Candy Store"},
"Candy Store": {"UP": "outdoors", "LEFT": "Tavern", "RIGHT": "Workshop", "DOWN": "Papermill"},
"Tavern": {"RIGHT": "Candy Store", "DOWN": "Empty House"},
"Empty House": {"UP": "Tavern", "RIGHT": "Papermill"},
"Papermill": {"UP": "Candy Store", "LEFT": "Empty House"},
}
items = {
"Flowerhaus": None,
"Tavern": None,
"Barn": "Car Key",
"Random Cabin": "Neighbor's Key",
"Magistrate's Home": "Deadbolt Key",
"Workshop": "Office Key",
"Candy Store": "Ex's Key",
"Empty House": "House Key",
"Papermill": "Mailbox Key"
}
current_space = "outdoors"
current_inventory = []
print("""It's been a long, long, frustrating day.
You've entered the front door to your home, ready to take your shoes off and relax... As you walk through the door, you realize this is not your house, your city, or even your realm of existence. Warily, you step forward to further take in your surroundings. The floor is dirt, the buildings around you look strange, and the people don't seem to react to your presence.
After a short time, you realize your keys are no longer in your hand. As you turn back around, you notice the door is still there, but now it has seven locks, each appearing to match the locks that go to all seven of your keys. Turning the knob proves the portal home is locked. Find all seven keys to unlock the door. Fail to find all seven keys and the door will disappear, leaving you trapped forever.
So, with all of that in mind, where would you like to look first?
""")
while not (len(current_inventory) == 7 and current_space == "outdoors"):
direction = input("Type UP, DOWN, LEFT, RIGHT, or QUIT: ").upper()
if direction in spaces[current_space]:
current_space = spaces[current_space][direction]
print(f"You walk through the door and enter the {current_space}.")
if items.get(current_space, None) and items[current_space] not in current_inventory: # Something new to collect
print(f"You spot the {items[current_space]} there and pick it up.")
current_inventory.append(items[current_space])
print("You carry", end=" ")
if not current_inventory:
print("an empty keyring.")
elif len(current_inventory) == 1:
print(f"the {current_inventory[0]} on your keyring.")
else:
print("the", " and the ".join((", the ".join(current_inventory[:-1]), current_inventory[-1])), "on your keyring.")
elif direction == "QUIT":
print("Oh... So, you're going to take a break then? Yeah, maybe a nap is a good idea. Try again when you're rested.")
break
else:
print("Nothing over here. Don't you want to get back home? Try a different direction.")
else: # Found all 7 keys
print("You unlock the magical door wih the seven keys, and walk out. You find yourself back on the street. Looking back it looks like all this was a bad dream, from which you barely managed to awaken.")
Upvotes: 1