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?

50 Views Asked by At

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:

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...")
3

There are 3 best solutions below

0
sudden_appearance On BEST ANSWER

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...")
0
Lajos Arpad On

You have here your outer loop:

    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...")

You get the player_input outside your loop and never change it. So when your access_inventory finishes its job, then it will get back into this loop and see open_inventory unchanged, so it will call the method again. Fix:

    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()
            open_inventory = False
        else:
            break
    print("Oy vey...")

also, player_input specifies what the input of the player is and open_inventory specifies whether the inventory is to be opened. Since their meaning overlaps, you might end up having confusions in the future. It's a good rule of thumb to make sure that you reduce the number of your variables and if possible have as few variables with the same meaning as possible.

0
weshouman On

You need to do 2 modifications

  1. Set to open_inventory global variable to False (the only output of access_inventory)

    while open_inventory:
        if player_input == "I":
            open_inventory = access_inventory()
        else:
            break
    
  2. Continue instead of breaking using inventory, a click of B should be used to exit and not just using the inventory elements.

    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:
                continue
        else:
            print("You have no more of this item!")
            continue