0

The problem breaks down to this. In my text based game, before you can enter the training grounds, you must give the hungry guard the bread from your inventory. As of now, that works. The inventory updates to show the bread is no longer in the inventory. Along with removing the bread from the inventory, it gets removed from the dictionary called rooms.

Everything works except for when you re-enter the training grounds later, It then asks you for the bread again, which no longer exists.

My question is how would I write a while, if, elif, etc statement like:

if "Bread" not in rooms["Kitchen"]["Item"]:
    continue

or something like that, so that you can re enter the room without it asking for the bread at a later point. I've only been coding for a few weeks so I know my code is probably garbage but I'm using this project to learn. The issue I face is the code needs to not allow you in if you don't have the bread for the guard, allow you to give the guard the bread for access, and then act like a regular room after you have given the guard the bread, leave, and then return to that room later.

here is the section of code.

if current_room == "Training Grounds" and "Bread" not in Inventory:
        print("A hungry guard blocks your path and shoves you back into the Great Hall.")
        current_room = "Great Hall"
        continue

    if current_room == "Training Grounds" and "Bread" in Inventory:
        player_move = input("Would you like to offer the guard bread in exchange for access to the Training Grounds? ")
        if player_move == "yes".capitalize():
            current_room = "Training Grounds"
        print("The guard lets you enter the Training Grounds.")
        Inventory.remove("Bread")
        del rooms["Kitchen"]["Item"]
        continue
    elif player_move == "no".capitalize():
        print('"Get the hell out of here!"')
        print("The guard shoves you back into the Great Hall.")
        current_room = "Great Hall"
        continue

I'm assuming somewhere in here is where I need the line to check for that, and allow you to enter the room. Or maybe my approach is off entirely. Any help is greatly appreciated.

I tried all sorts of variations of that solution. Wording it different ways, even tried using things like first_time_in_room = True however, I don't really know how those work entirely.

Krondor12
  • 1
  • 2
  • How did you initialize _the dictionary called rooms_? – Armali Jun 28 '23 at 10:19
  • 1
    ```rooms = { "Castle Bridge": {"North": "Castle Gate"}, "Castle Gate": {"North": "Great Hall", "South": "Castle Bridge", "Item": "Gold Coins"},``` – Krondor12 Jun 29 '23 at 15:25

1 Answers1

0

Along with removing the bread from the inventory, it gets removed from the dictionary called rooms.

It would be logical not when removing the bread from the inventory, it gets removed from the dictionary called rooms, but rather when the bread is picked up from the kitchen into the inventory.

My question is how would I write a while, if, elif, etc statement like: if "Bread" not in rooms["Kitchen"]["Item"]: continue or something like that, so that you can re enter the room without it asking for the bread at a later point.

It is not quite relevant whether the bread is in the kitchen; pivotal is whether the bread has been given to the guard.

tried using things like first_time_in_room = True

If you have initialized that, and if you set it to False when the guard was given the bread, you can write:

    if current_room == "Training Grounds" and "Bread" not in Inventory:
      if first_time_in_room:
        print("A hungry guard blocks your path and shoves you back into the Great Hall.")
        …
Armali
  • 18,255
  • 14
  • 57
  • 171