2016-02-12 75 views
0

这是我的脚本...词典最新

shop = ["Apple" , "orange", "mangoo" , "Tomato" , "grape"] 
dict = {} 
#List Shop 

x = """ 
1. apple 
2. orange 
3. mangoo 
4. tomato 
5. grape """ 
print x 
#Screen showing in monitor 

while True: 

    buy = int(raw_input('what do u want to buy?" (use number 1-7): ')) 
    buy = shop[buy-1] 
    print "You want" , buy 
    equal = int(raw_input('how much? : ')) 
    bb = {buy:equal} 
    dict.update(bb) 
    print dict, x 

如果我选择了相同的顺序。旧值将与最新的数值变化......

我想做到最好的,像这样...

我选择了“苹果”,我想在接下来的指令买入“3”

我想要再次购买“苹果”

但我会购买“6”。 等于苹果是“9”...

注意:python 2.7,此方法不使用zip,def,return和break选项。

回答

2

注:dict是保留字典的关键字。使用类似my_dict的东西。

试试这个:

my_dict[buy] = my_dict.get(buy, 0) + equal 

语法dict.get的:

dict.get(key[,default]) 

代码:

shop = ["Apple" , "orange", "mangoo" , "Tomato" , "grape"] 
my_dict = {} 
#List Shop 

x = """ 
1. apple 
2. orange 
3. mangoo 
4. tomato 
5. grape """ 
print x 
#Screen showing in monitor 

while True:  
    buy = int(raw_input('what do u want to buy?" (use number 1-7): ')) 
    buy = shop[buy-1] 
    print "You want" , buy 
    equal = int(raw_input('how much? : ')) 
    my_dict[buy] = my_dict.get(buy, 0) + equal 
    print my_dict, x 
+0

如果我必须把该代码? – Helonder777

+0

@ Helonder777我更新了整个代码,看到我更新后的文章 – Hackaholic

+1

谢谢兄弟,这项工作对我来说...非常感谢:) – Helonder777