我正在创建一个基于文本的游戏/视觉小说,并列出了一份 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...")