一、元素分类
有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。
即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}
list = [11,22,33,44,55,66,77,88,99,90]
dict ={'k1':[] , 'k2':[]}
for x in list:
if x >66:
dict.get('k1').append(x)
else:
dict.get('k2').append(x)
print(dict)
二、查找
查找列表中元素,移除每个元素的空格,并查找以 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 = ["alec", " aric", "Alex", "Tony", "rain"]
for i in range(len(li)):
li[i]=li[i].strip()
if (li[i].startswith('a') or li[i].startswith('A')) and li[i].endswith('c'):
print(li[i])
print(li)
tu = ("alec", " aric", "Alex", "Tony", "rain")
z = list(tu)
for i in range(len(z)):
z[i] = z[i].strip()
if (z[i].startswith('a') or z[i].startswith('A')) and z[i].endswith('c'):
print(z[i])
tu = tuple(z)
print(tu)
dic = {'k1': "alex", 'k2': ' aric', "k3": "Alex", "k4": "Tony"}
for i in dic:
key = dic[i]
key = key.strip()
dic[i] = key
if (key.startswith('a') or key.startswith('A')) and key.endswith('c'):
print(key)
print(dic)
三、
输出商品列表,用户输入序号,显示用户选中的商品。
li = ["手机", "电脑", '鼠标垫', '游艇']
for i,j in enumerate(li):
print(i+1,j,end=' ')
while True:
a = (input('\n请输入商品序号:'))
if a.isspace() or not a:
print('输入为空,请重新输入:')
continue
elif a.isdigit() == True :
break
else:
print('输入非数字,请重新输入:')
continue
print('所选商品:', li[int(a) - 1])
四、购物车
功能要求:
要求用户输入总资产,例如:2000
显示商品列表,让用户根据序号选择商品,加入购物车
购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
附加:可充值、某商品移除购物车
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]
shopcart = [] #定义购物车为空列表
tag = True
while tag:
money = input('请输入总资产:')
if money.isdigit(): #判断输入是否为数字
money = int(money)
else:
print('输入有误,请重新输入!')
continue
while tag:
print('商品列表'.center(46,'='))
for i,j in enumerate(goods):
print(i+1,j['name'],j['price']) #商品序号 起始0+1
choice = input('请选择商品序号(充值:w 移除商品:e 查看购物车:r 结算:t 退出:q):')
if choice.isdigit():
choice = int(choice)
if choice <= len(goods) and choice !=0: #在序号范围内且不为0
item = goods[choice - 1] #对应 序号-1
if item['price'] <= money: #商品总额不大于总资产
shopcart.append(item) #加入购物车
money -= item['price']
print('选购列表'.center(46,'-'))
for list in shopcart:
print('[',list['name'],list['price'],']',end=' ')
print('\n','当前余额为%s' %money )
else:
print('余额为%s,无法购买,请充值。' %money)
else:
print('输入有误,请重新输入!')
elif choice == 'w': #充值
while True:
print('充值'.center(48, '='))
money1 = input('请输入充值金额(返回:b):')
if money1 == 'b':
break
if money1.isdigit():
money += int(money1) #类型转换
print('当前可用余额为:%s' %money)
break
else:
print('输入有误,请重新输入!')
elif choice == 'e': #修改购物车
while True:
print('移除商品'.center(46, '='))
for m,n in enumerate(shopcart):
print(m + 1,'[',n['name'],n['price'],']')
remove = input('请输入移除商品序号(返回:b):')
if remove == 'b':
break
if remove.isdigit():
remove = int(remove)
if remove <= len(shopcart) and remove !=0:
item1 = shopcart[remove - 1]
del shopcart[remove - 1]
money += item1['price']
print('选购列表'.center(46,'-'))
for list1 in shopcart:
print('[',list1['name'],list1['price'],']',end=' ,')
print('\n','已移除商品[%s],当前可用余额为:%s' %(item1['name'] , money))
if len(shopcart) == 0:
print('当前购物车为空,返回商品列表。')
break
else:
print('输入有误,请输入商品序号!')
elif choice == 'r': #查看购物车
while True:
print('查看购物车'.center(45, '='))
for x, y in enumerate(shopcart):
print(x + 1, '[', y['name'], y['price'], ']')
print('余额:%s' % money)
inp = input('输入[b]返回:')
if inp == 'b':
break
else:
print('输入有误,请重新输入!')
elif choice == 't': #商品结算
print('结算'.center(48,'='))
for list in shopcart:
print('[',list['name'], list['price'],']') #显示选购商品
print('余额:%s' % money)
print(50 * '=')
print('欢迎下次光临!')
tag = False #退出循环
elif choice == 'q':
print(50 * '=')
print('欢迎下次光临!')
tag = False
else:
print('输入有误,请重新输入!')
选做题:用户交互,显示省市县三级联动的选择
dic = {
"河北": {
"石家庄": ["鹿泉", "藁城", "元氏"],
"邯郸": ["永年", "涉县", "磁县"],
}
"河南": {
...
}
"山西": {
...
}
}