交互式商店系统不工作

2024-10-01 22:41:27 发布

您现在位置:Python中文网/ 问答频道 /正文

money = 0    
Pickaxes = {
 'adamant pickaxe': {'cost': 100, 'speed': 5},
 'bronze pickaxe': {'cost': 100, 'speed': 5},
 'dragon pickaxe': {'cost': 100, 'speed': 5},
 'inferno adze  ': {'cost': 100, 'speed': 5},
 'iron pickaxe': {'cost': 100, 'speed': 5},
 'mithril pickaxe': {'cost': 100, 'speed': 5},
 'rune pickaxe': {'cost': 100, 'speed': 5},
 'steel pickaxe': {'cost': 100, 'speed': 5}}

def shop():
    global cost
    print "Welcome to Alister's goods!"
    time.sleep(2)
    sellbuy = raw_input("\nWould you like to sell or buy goods?:\n")
        if sellbuy == "sell":
            sell()
        else:
            print "\nHere is a list of our goods....\n"
            for i in Pickaxes:
                print i
            what_item = raw_input("\nWhich item would you like to purchase?\n")
            if what_item in Pickaxes and money >= Pickaxes[what_item]["cost"]:
                    inventory.append[what_item]
                    print "You have successfully purchased a", what_item
                    start()

我的商店定义停止工作时,你键入的项目,你想在原始输入它只是停止程序没有错误,这是一个更大的程序的一部分,这就是为什么它在一个定义等。。。。你知道吗

谢谢


Tags: toyouinputrawitemwhatspeedprint
1条回答
网友
1楼 · 发布于 2024-10-01 22:41:27

根据你的代码,钱是0,这意味着它永远不会进入那个条件,程序只是退出。(您可能还想添加一些东西,以减少购买后的钱,而你是在它)

你应该抓住那个条件

if what_item in Pickaxes:
    if money >= Pickaxes[what_item]["cost"]:
        inventory.append[what_item]
        print "You have successfully purchased a", what_item
        start()
    else:
        print "Not enough money"
        start()
else:
    print "No such item"
    start()

或者在一段时间内循环购买:

while True:
    what_item = raw_input("\nWhich item would you like to purchase? (Leave blank to quit)\n")
    if not what_item:
        break # exit the loop
    if what_item in Pickaxes:
        if money >= Pickaxes[what_item]["cost"]:
            inventory.append[what_item]
            print "You have successfully purchased a", what_item
            break # exits the loop
        else:
            print "Not enough money"
    else:
        print "No such item"

相关问题 更多 >

    热门问题