day07 作业

# 元组练习题:
#简单购物车,要求如下:
# 实现打印商品详细信息,用户输入商品名和购买个数,则将商品名,价格,购买个数加入购物列表,如果输入为空或其他非法输入则要求用户重新输入  
msg_dic={
'apple':1,
'tesla':100000,
'mac':3000,
'lenovo':30000,
'chicken':10,
}
while True:
for key,value in msg_dic.items():
print(f'商品名称:{key} 单价:{value}元')
inp_name=input('请输入商品的名称:').strip()
if inp_name not in msg_dic:
print('输入的商品不存在,请重新输入:')
continue
count=input('请输入购买商品的个数:').strip()
if count.isdigit():
count=int(count)
shop_name=msg_dic.get(inp_name)
cost=shop_name*count
print(shop_name)




# 字典练习题:
# 1 有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中
# 即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}
#list1=[11,22,33,44,55,66,77,88,99,90]
dict1={'k1':[],'k2':[]}
for line in list1:
if line > 66:
dict1['k1'].append(line)
elif line < 66:
dict1['k2'].append(line)
print(dict1)
# 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'
new_list = s.split(' ')
print(new_list)
dic = {}
for line in new_list:
if line not in dic:
dic[line] = 1
else:
dic[line] += 1
print(dic)
原文地址:https://www.cnblogs.com/suyuanyuan/p/13080947.html