互动商店系统不工作

时间:2013-12-23 13:41:39

标签: python list tuples global

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

当你在raw_input中键入你想要的项目时,我的商店定义停止工作它只是停止程序没有错误,这是一个更大的程序的一部分,这就是为什么它在定义等等....

由于

1 个答案:

答案 0 :(得分:2)

根据你的代码,钱是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"