day7作业

元组练习题,字典练习题

元组练习题:

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

msg_dic={
'apple':10,
'tesla':100000,
'mac':3000,
'lenovo':30000,
'chicken':10,
}
msg_dic={
    'apple':10,
    'tesla':100000,
    'mac':3000,
    'lenovo':30000,
    'chicken':10,
    }

l = []
sum = 0
while True:
	product = input("请输入购买商品:")
	product.lower().strip()
	#判断商品是不是已有商品
	if product in msg_dic:
		num = int(input("请输入商品个数:"))
		quit = input("结束购物,请按q,继续购物,请按a:")
		price = msg_dic[product]
		total = num * price
		sum += total
		res = (product,price,num,total)
		print(res)
		l.append(res)
		# 按q退出
		if quit =="q":
			print(l)
			print("总价为:%s"%(sum))
			sign=False
			break
	else:
		print("非法输入,请重新输入")

字典练习题:

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

即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}
l = [11,22,33,44,55,66,77,88,99,90]
d = {'k1':[],'k2':[]}
for i in l:
	if i > 66:
		d['k1'].append(i)
	elif i < 66:
		d['k2'].append(i)
print(d)
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'
l = s.split(' ')
d = {}
for i in l:
	res = l.count(i)
	if i not in d:
		d[i] = res
print(d)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值