六月九号作业

元组练习题:
#简单购物车,要求如下:
实现打印商品详细信息,用户输入商品名和购买个数,则将商品名,价格,购买个数加入购物列表,如果输入为空或其他非法输入则要求用户重新输入  

msg_dic = {
'apple': 10,
'tesla': 100000,
'mac': 3000,
'lenovo': 30000,
'chicken': 10,
}
i = []
while True:
x = input('请输入商品名称:')
if x not in msg_dic:
print('商品不存在')
continue
else:
while True:
y = input('购买个数:').strip()
if y.isdigit():
y = int(y)
print('完成添加购物车')
z = msg_dic[x] * y
break
else:
print('输入有误')
i.append((x,y,z))
print(i)



字典练习题:
1 有如下值集合 [11,22,33,44,55,66,77,88,99,90...],
将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中

即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}
k1 = []
k2 = []
for i in [11,22,33,44,55,66,77,88,99,90]:
if i > 66:
k1.append(i)
else:
k2.append(i)
print({'k1':k1,'k2':k2})


2 统计s='hello alex alex say hello sb sb'中每个单词的个数
结果如:{'hello': 2, 'alex': 2, 'say': 1, 'sb': 2}
s='hello alex alex say hello sb sb'
h = (s.count('hello'))
a = (s.count('alex'))
sa = (s.count('say'))
sb = (s.count('sb'))
print({"hello":h,"alex":a,"say":sa,"sb":sb})
posted @ 2020-06-09 18:06  最冷不过冬夜  阅读(131)  评论(0编辑  收藏  举报