<span style="font-family:FangSong_GB2312;font-size:18px;"> </span>
<span style="font-family:FangSong_GB2312;font-size:18px;"> #!/usr/bin/env python # -*- coding:utf-8 -*- #一、元素分类 #有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。 #即: {'k1': 大于66的所有值, 'k2': 小于66的所有值} li = [11,22,33,44,55,66,77,88,99] dic = { "k1":[], "k2":[] } for i in li: if i <= 66: dic["k1"].append(i) else: dic["k2"].append(i) print(dic)</span>
二、
<span style="font-family:FangSong_GB2312;font-size:18px;"> #!/usr/bin/env python # -*- coding:utf-8 -*- #一、元素分类 #有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。 #即: {'k1': 大于66的所有值, 'k2': 小于66的所有值} li = [11,22,33,44,55,66,77,88,99] dic = { "k1":[], "k2":[] } for i in li: if i <= 66: dic["k1"].append(i) else: dic["k2"].append(i) print(dic)</span>
<span style="font-family:FangSong_GB2312;font-size:18px;"> #!/usr/bin/env python # -*- coding:utf-8 -*- #三、输出商品列表,用户输入序号,显示用户选中的商品 # 商品 li = ["手机", "电脑", '鼠标垫', '游艇'] #思路: #a.给每个商品添加序列,遍历出来 #b.用户输入,并转换成int #c.判断,输入序号在一个范围内就输入(用索引的方式取值) li = ["手机", "电脑", '鼠标垫', '游艇'] for i,j in enumerate(li): print(i+1,j) #enumerate的初始值是从0开始的,所以加1 inp_num = int(input("输入序号:")) le = len(li) if inp_num > 0 and inp_num <=le : print(li[inp_num-1])#因为上面加1了,对应的值就不对了,所以要减1;如果输入的索引得到的值不在列表里呢?做判断 else: print("小伙子你是不是懵逼了:|") </span>
<span style="font-family:FangSong_GB2312;font-size:18px;"> #!/usr/bin/env python # -*- coding:utf-8 -*- #四、购物车 #功能要求: # 要求用户输入总资产,例如:2000 # 显示商品列表,让用户根据序号选择商品,加入购物车 # 购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。 # 附加:可充值、某商品移除购物车 #goods = [ # {"name": "电脑", "price": 1999}, # {"name": "鼠标", "price": 10}, # {"name": "游艇", "price": 20}, # {"name": "美女", "price": 998}, #] #思路: #a.列出商品的名称和价格 #b.输出总资产 #c.购买商品 #d.购买商品的商品写入到一个字典中 #f.判断输入商品在不在字典里面 #e.是否结束购买 #g.计算结果做判断 inp_all = int(input("输入你的总资产:")) goods = [ {"name": "电脑", "price": 1999}, {"name": "鼠标", "price": 10}, {"name": "游艇", "price": 20}, {"name": "美女", "price": 998}, ] for i in goods: print(i["name"],i["price"]) dic_cit = {} #定义一个字典等待用户购买的商品输入 做准备 while True: inp_shp = input("输入你需要的商品(Y/y结账):") if inp_shp.lower() == "y": break for item in goods: #在遍历一次字典 if item["name"] == inp_shp: #索引根据字典的name得到商品名称 name1 = item["name"] if name1 in dic_cit.keys(): dic_cit[name1]['num'] = dic_cit[name1]['num'] + 1 else: dic_cit[name1] = {"num":1,'single.price':item["price"]} print(dic_cit) all_si = 0 for k,v in dic_cit.items(): print(k,v) n = v["single.price"] m = v["num"] mon_al = n * m all_si = all_si + mon_al if all_si > inp_all: print("钱不够") else: print(购买成功!) </span>
#!/usr/bin/env python #-*- coding: utf-8 -*- dic = { "河北": { "石家庄": ["鹿泉", "藁城", "元氏"], "邯郸": ["永年", "涉县", "磁县"], }, "河南": { "郑州": ["中原区","二七区","荥阳市"], "开封": ["龙亭区","鼓楼区","兰考县"], }, "山西": { "太原": ["杏花岭","清徐","小店"], "大同": ["城区","南郊","广陵"], } } ks = dic.keys() print("河北,山西,河南")# ["河北", 22,33,44] for i in ks: in_pro = input("please input province:") if in_pro == "": print("重新输入") continue ks_2ct = dic[in_pro] # print(ks) #dict_keys(['河北', '山西', '河南'])输入做判断的根本 # print(ks_2ct)#{'石家庄': ['鹿泉', '藁城', '元氏'], '邯郸': ['永年', '涉县', '磁县']} ks2 = ks_2ct.keys() for j in ks2: print(j) in_ct = input("please input city: ") if in_ct == "": print("空值,重新输入") continue ks3 = ks_2ct[in_ct] print(ks2) print(type(ks2)) for r in ks3: print(r) # print(ks3) #ks[input_num] => "河北" #dic['河北'] dict_keys dict.keys()