#1.有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。
#即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}
b = [11, 22, 33, 44, 55, 66, 77, 88, 99, 90]
a = {'k1':[],'k2':[]}
for i in b:
if i <= 66:
a['k1'].append(i)
else:
a['k2'].append(i)
print(a)
#2.查找列表中元素,移除每个元素的空格,并查找以 a或A开头 并且以 c 结尾的所有元素。
li = ["alec", " aric", "Alex", "Tony", "rain"]
tu = ("alec", " aric", "Alex", "Tony", "rain")
dic = {'k1': "alex", 'k2': ' aric', "k3": "Alex", "k4": "Tony"}
for i in li:
if (i.strip().startswith('a') or i.strip().startswith('A')) and i.strip().endswith('c'):
print(i.strip())
for i in dic.values():
if (i.strip().startswith('a') or i.strip().startswith('A')) and i.strip().endswith('c'):
print(i.strip())
#3.输出商品列表,用户输入序号,显示用户选中的商品
li = ["手机", "电脑", '鼠标垫', '游艇']
for k,v in enumerate(li):
print(str(k) +' : ' +v)
a = int(input("您选择的商品序号序号:"))
if a > 0 and a < len(li):
print('您选择的商品是:' + li[a])
else:
print('商品不存在')
#4.购物车
#功能要求:要求用户输入总资产,例如:2000,显示商品列表,让用户根据序号选择商品,加入购物车,购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
#附加:可充值、某商品移除购物车
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]
Shopping = []
money = 0
sum_all= 0
credit = int(input('您现在有:' + '0 元,请输入您充值的金额:'))
print('您本次预存:' + str(credit) + '元')
while True:
for o, p in enumerate(goods):
print('商品序号' + str(o), ':' + p['name'] + '; 价格:' + str(p['price']))
a = input("请输入您选择的商品序号[输入q结算]:")
if a.isdigit():
if int(a) > o:
print('商品不存在,请重新选择!')
elif int(a) <= o:
sum_all += goods[int(a)]['price']
if credit >= sum_all:
Shopping.append(goods[int(a)]['name'])
money += goods[int(a)]['price']
print('商品:'+ goods[int(a)]['name'] +';已加入购物车,购物车清单:[' + ' '.join(Shopping) + '] 预计消费' + str(sum_all) +'元')
else:
print("余额不足!购物车清单:[" + ' '.join(Shopping)+ "], 您的预存金额为:" + str(credit) + '元,您预计消费为:' + str(sum_all) +'元;差' + str(sum_all-credit) +'元')
sum_all -= goods[int(a)]['price']
c = input("是否继续充值(y是/n否):")
if c == 'y':
d = input("请输入充值金额:")
credit += int(d)
print("您充值了%s元"%d)
elif c == 'n':
continue
else:
print("输入错误")
else:
print('输入错误!请输入您选择的商品序号[输入q结算]:')
if a.lower() == 'q':
print('您的购物清单如下:[%s];共消费:%s元;剩余:%s元' % (' '.join(Shopping), money, (credit - money)))
break
#5、用户交互,显示省市县三级联动的选择
dic = {
"河北": {
"石家庄": ["鹿泉", "藁城", "元氏"],
"邯郸": ["永年", "涉县", "磁县"],
},
"河南": {
"郑州":["中原","金水","中牟","二七"],
"商丘":["虞城","夏邑","宁陵","永城"],
},
"山西": {
"太原":["小店","古交","迎泽"],
"大同":["平城","云冈","广灵"],
}
}
while True:
for i in dic:
print(i)
sheng = input("请输入省份:")
for k in dic[sheng]:
print(k)
shi = input("请输入市,退出按q:")
if shi.lower() == 'q':
break
for j in dic[sheng][shi]:
print(j)
print('已经到底了,请继续选择省份!')