Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I created a text-based game with an inventory but I can't seem to exit the inventory when prompted. How do I exit it?

I’m creating a text-based game/visual novel with an inventory. When prompted, I can access the inventory fine, it detects if there are no items, but if I try to press B to exit, the inventory loops in on itself. How do I fix that?

#adventure game
import time, sys;

health = 5
energy = 0
inventory = {"COFFEE": 1, "BOOSTER DRUG": 0}
open_inventory = False

def sleep(sleep_sec):
    time.sleep(sleep_sec)

Accessing inventory with bugproofing:

def access_inventory():
    while True:
        print("              Inventory:        ")
        print("--------------------------------")
        for item, item_count in inventory.items():
            print(item,":", item_count)
        print("--------------------------------")
        print("Enter the item you want to use, then press 'B' to exit\n")
            
        item_choice = input().upper()
        
        if item_choice in inventory:
            if inventory[item_choice] != 0:
                print("Use", item_choice,"? Y/N")
                confirm_choice = input().upper()
                
                if confirm_choice == "Y":
                    inventory[item_choice] -= 1
                    #energy +=1
                    print("You have gained 1 energy!")
                else:
                    break
            else:
                print("You have no more of this item!")
                break

        elif item_choice=="B":
            return False 
        else:
            print("Invalid choice")

first scene:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

def first_scene():
    sleep(1)
    print("The door opens to a neglected room, filled with melancholy and decay. The scent of coffee and unwashed clothes permate the air.")
    sleep(1)
    print("Papers cover the walls, the ratty bed, and the stained desk. Slumped face-down on the desk is a figure draped in a dirty lab coat.")
    sleep(1)
    print("The figure moans and raises his head, a hand reaching over to rub his forehead")
    sleep(1)
    print("'Coffee...'")
    sleep(1)
    player_input = input("Prepare a cup of coffee by accessing your inventory with 'I'.\nOnce you are done, press 'b' to return.\n").upper()
    open_inventory = True
    while open_inventory:
        if player_input == "I":
            access_inventory()
        else:
            break
    print("Oy vey...")

>Solution :

Your player_input is always the first input (I in case you open inventory), so it never breaks

There are multiple solutions for that, but in general you either need to change player_input or break explicitly. So either

open_inventory = True
while open_inventory:
    player_input = input("Prepare a cup of coffee by accessing your inventory with 'I'.\nOnce you are done, press 'b' to return.\n").upper()
    if player_input == "I":
        access_inventory()
    else:
        break
print("Oy vey...")

or

while open_inventory:
    if player_input == "I":
        access_inventory()
        break
print("Oy vey...")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading