python之字典

基本操作:

 1 # Author:yebo
 2 
 3 info = {
 4     'stu01':"yebo",
 5     'stu02':"shuz",
 6     'stu03':"yomi"
 7 }
 8 
 9 
10 #
11 info['stu01'] = "松江小旋风"
12 
13 #
14 info['stu04'] = "7zai"
15 print(info)
16 
17 #
18 info.pop('stu02')
19 print(info)
20 
21 #
22 print(info.get('stu01'))
23 print('stu01' in info)   #判断有无此key值
24 
25 #循环打印
26 for i in info:
27     print(i,info[i])   #法一,更好
28 
29 for k,v in info.items():
30     print(k,v)   #法二

三级菜单:

 1 # Author:yebo
 2 
 3 data = {
 4     'shanghai' : {
 5         'songjiang' :{
 6             "sues" : ["ysh","zcx"]   #最后用[]或者{}都可以
 7         },
 8         'xuhui' :{
 9             "jiaoda" : {"jb"}
10         }
11     },
12     'changsha' : {
13         'yuelu' :{
14             "huzhongda" :{"shuzhui","yomi"},
15             "hunanshida" : {"yt"},
16             "zhongnan" : {"shuzhe"}
17         },
18         'kaifu' :{
19             "changli" : {"kzr"}
20         }
21     }
22 }
23 
24 exit_flag = False
25 
26 while not exit_flag:   #重点!!!!!!
27     for i in data:
28         print(i)   #打印一级菜单
29     choice = input("选择进入1>>>:")   #进入第一级菜单
30     if choice in data:
31         while not exit_flag:
32             for i2 in data[choice]:
33                 print("\t",i2)   #打印二级菜单
34             choice2 = input("选择进入2>>>:")   #进入第二级菜单
35             if choice2 in data[choice]:
36                 while not exit_flag:
37                     for i3 in data[choice][choice2]:
38                         print("\t\t", i3)   #打印三级菜单
39                     choice3 = input("选择进入3>>>:")   #进入第三级菜单
40                     if choice3 in data[choice][choice2]:
41                         for i4 in data[choice][choice2][choice3]:
42                             print("\t\t\t", i4)   #打印三级菜单中内容
43                         choice4 = input("最后一层,按b返回>>>:")
44                         if choice4 == 'b':
45                             pass
46                         elif choice4 == 'q':
47                             exit_flag = True
48                     if choice3 == 'b':
49                         break
50                     elif choice3 == 'q':
51                         exit_flag = True
52             if choice2 == 'b':
53                 break
54             elif choice2 == 'q':
55                 exit_flag = True
56     elif choice == 'q':
57         exit_flag = True

购物车:

自己的垃圾版本:(缺少许多的用户输入验证if判断)

 1 # Author:yebo
 2 
 3 
 4 items = [
 5     [1,"iphone",5800],
 6     [2,"mac pro",12000],
 7     [3,"latte",31],
 8     [4,"python start",81],
 9     [5,"bike",800]
10 ]
11 shopping_list = []   #提前准备一个空集
12 
13 my_salary = int(input("my salary:"))
14 
15 while True:
16     print(items)
17     items_number = int(input("which one do you want to buy?"))
18     items_index = items_number - 1   #实际列表排序从0开始
19 
20     if items[items_index][2] > my_salary:
21         print("you do not have enough money")
22     else:
23         my_salary = my_salary - items[items_index][2]
24         print("you have bought %s,which is %d, my salary remain:%d"
25               %(items[items_index][1],items[items_index][2],my_salary))
26         shopping_list.append(items[items_index][1])
27 
28     continue_confirm = input("if you want to quit, please press 'q'; if not, please press any key.")
29     if continue_confirm == "q":
30         break
31 
32 print("***----list----***")
33 print("you have bought:")
34 for i in shopping_list:
35     print(i)
36 print("***------------***")

老男孩ALex老师版本:

 1 __author__ = "Alex Li"
 2 
 3 
 4 product_list = [
 5     ('Iphone',5800),
 6     ('Mac Pro',12000),
 7     ('Bike',800),
 8     ('Watch',30000),
 9     ('Coffee',30),
10     ('Alex Python',120),
11 ]
12 shopping_list = []
13 salary = input("Input your salary:")
14 if salary.isdigit():   #判断输入是否是数字
15     salary = int(salary)
16     while True:
17         for index,item in enumerate(product_list):   #方法一:enumerate在同时需要index和value的时候使用
18             #print(product_list.index(item),item)  #方法二:用.index增加了商品编号
19             print(index,item)
20         user_choice = input("选择要买嘛?>>>:")
21         if user_choice.isdigit():   #判断输入是否是数字
22             user_choice = int(user_choice)
23             if 0 <= user_choice < len(product_list):   #len()直接得到列表长度
24                 p_item = product_list[user_choice]   #取出购买的商品
25                 if p_item[1] <= salary:  #买的起
26                     shopping_list.append(p_item)   #增加到购物车
27                     salary -= p_item[1]
28                     print("Added %s into shopping cart,your current balance is \033[31;1m%s\033[0m" %(p_item,salary) )
29                 else:
30                     print("\033[41;1m你的余额只剩[%s]啦,还买个毛线\033[0m" % salary)   #增加字体颜色
31             else:
32                 print("product code [%s] is not exist!"% user_choice)
33         elif user_choice == 'q':
34             print("--------shopping list------")
35             for p in shopping_list:
36                 print(p)
37             print("Your current balance:",salary)
38             exit()
39         else:
40             print("invalid option")

 

posted @ 2018-08-16 22:01  松江小旋风  阅读(137)  评论(0)    收藏  举报