day4作业
Posted ezway
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了day4作业相关的知识,希望对你有一定的参考价值。
练习题
一、元素分类
有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。
即: {\'k1\': 大于66的所有值, \'k2\': 小于66的所有值}
二、查找
查找列表中元素,移除每个元素的空格,并查找以 a或A开头 并且以 c 结尾的所有元素。
li = ["alec", " aric", "Alex", "Tony", "rain"]
tu = ("alec", " aric", "Alex", "Tony", "rain")
dic = {\'k1\': "alex", \'k2\': \' aric\', "k3": "Alex", "k4": "Tony"}
三、输出商品列表,用户输入序号,显示用户选中的商品
商品 li = ["手机", "电脑", \'鼠标垫\', \'游艇\']
四、购物车
功能要求:
- 要求用户输入总资产,例如:2000
- 显示商品列表,让用户根据序号选择商品,加入购物车
- 购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
- 附加:可充值、某商品移除购物车
1
2
3
4
5
6
|
goods = [ { "name" : "电脑" , "price" : 1999 }, { "name" : "鼠标" , "price" : 10 }, { "name" : "游艇" , "price" : 20 }, { "name" : "美女" , "price" : 998 }, ] |
五、用户交互,显示省市县三级联动的选择
1
2
3
4
5
6
7
8
9
10
11
12
13
|
dic = { "河北" : { "石家庄" : [ "鹿泉" , "藁城" , "元氏" ], "邯郸" : [ "永年" , "涉县" , "磁县" ], } "河南" : { ... } "山西" : { ... } } |
答案:
#!usr/bin/evn python # -*- coding:utf-8 -*- #1 dic={\'k1\':[],\'k2\':[]} li=[11,22,33,44,55,66,77,88,99,90] for i in li: if i>=66: dic[\'k1\'].append(i) else: dic[\'k2\'].append(i) print(dic) #2 li = ["aleb", " aric", "Alex", "Tony", "rain"] tu = ("alec", " aric", "Alex", "Tony", "rain") dic = {\'k1\': "alex", \'k2\': \' aric\', "k3": "Alex", "k4": "Tony"} for i in li: a=i.strip() if (a.startswith(\'a\') or a.startswith(\'A\')) and a.endswith(\'c\'): print(a) for j in tu: b=j.strip() if b.startswith(\'a\') or b.startswith(\'A\') and b.endswith(\'c\'): print(\'b-\'+b) for v in dic.values(): d=v.strip() #print(d,\'-\') if (d.startswith(\'a\') or b.startswith(\'A\')) and b.endswith(\'c\'): print(\'d-\'+d) #3 li = ["手机", "电脑", \'鼠标垫\', \'游艇\'] # a={} # for i,j in enumerate(li,1): # # a[i]=j # # num=int(input(\'blabla:\')) # # print(a[num]) for i, j in enumerate(li, 1): print(i, j) while 1: num = int(input(\'blabla:\')) if num > 0 and num <= len(li): print(li[num - 1]) else: print(\'null\') #4 total_money = int(input(\'请输入总金额: \')) picked_car = [] picked_money = 0 goods = [ {"name": "电脑", "price": 1999}, {"name": "鼠标", "price": 10}, {"name": "游艇", "price": 20}, {"name": "美女", "price": 998}, ] goods_name = [] goods_name_price = {} for i in goods: a = i[\'name\'] b = i[\'price\'] goods_name.append(a) # print(a) goods_name_price[a] = b # print(goods_name_price) for j, k in enumerate(goods_name, 1): print(j, k) while 1: goods_num = input(\'请输入选中商品号,付款请输入【购买】: \') try: goods_num = int(goods_num) except ValueError: goods_num = \'购买\' try: picked_car.append(goods_name[goods_num - 1]) picked_money += goods_name_price[goods_name[goods_num - 1]] except TypeError: pass print(picked_money) print(picked_car) if goods_num == \'购买\' and total_money >= picked_money: print(\'购买成功,谢谢惠顾\') break elif goods_num == \'购买\' and total_money < picked_money: user_choice = input(\'您的资金不足,请【充值】或者【移除】购物车内一些商品:\') if user_choice == \'充值\': print(\'当前余额:\' + total_money) total_money += int(input(\'请输入总金额: \')) print(\'充值后金额:\' + total_money) continue elif user_choice == \'移除\': while 1: print(picked_car) del_choice = input(\'请输入要移除的商品,完事后输出【ok】继续付款:\') del picked_car[del_choice] print(picked_car) if del_choice == \'ok\': break continue #5 dic = { "河北": { "石家庄": ["鹿泉", "藁城", "元氏"], "邯郸": ["永年", "涉县", "磁县"], } } for i in dic: print(i) a=dic[i] usip=str(input(\'输入省份: \')) for j in a: print(j) b=dic[i][j] usip2=str(input(\'输入市: \')) for k in b: print(k) usip2=str(input(\'输入县: \'))
以上是关于day4作业的主要内容,如果未能解决你的问题,请参考以下文章