我正在创建一个基于文本的游戏/视觉小说,并列出了一份 list .当出现提示时,我可以访问库存,它会检测是否没有物品,但如果我try 按B退出,库存就会自动循环.我该怎么解决这个问题呢?

#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)

使用防漏洞功能访问库存:

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

第一场戏:

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

推荐答案

你的player_input总是第一个输入的(I以防你打开库存),所以它永远不会破

有多种解决方案,但通常需要显式更改player_inputbreak.所以要么

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

while open_invent或y:
    if player_input == "I":
        access_invent或y()
        break
print("Oy vey...")

Python相关问答推荐

在Transformer中使用LabelEncoding的ML模型管道

删除pandas rame时间序列列中未更改的值

pyramid 内部数组中的连续序列-两极

NumPy中的右矩阵划分,还有比NP.linalg.inv()更好的方法吗?

如何根据条件在多指标框架上进行groupby

用gekko解决的ADE方程系统突然不再工作,错误消息异常:@错误:模型文件未找到.& &

如何根据另一列值用字典中的值替换列值

如何在具有重复数据的pandas中对groupby进行总和,同时保留其他列

如何让剧作家等待Python中出现特定cookie(然后返回它)?

ModuleNotFound错误:没有名为flags.State的模块; flags不是包

标题:如何在Python中使用嵌套饼图可视化分层数据?

如何使用html从excel中提取条件格式规则列表?

Odoo 16使用NTFS使字段只读

计算分布的标准差

如何启动下载并在不击中磁盘的情况下呈现响应?

Python Pandas获取层次路径直到顶层管理

与命令行相比,相同的Python代码在Companyter Notebook中运行速度慢20倍

pandas:对多级列框架的列进行排序/重新排序

mdates定位器在图表中显示不存在的时间间隔

解决Geopandas和Altair中的正图和投影问题