查缺补漏,对知识点和练习题进行归纳总结
朱忠原
1. 让用户输入金额
2. 选择要购买的商品加入购物车
3. 当商品的总价超过了你的金额提示余额不足
4. 让用户输入N结算,输入Q退出
5. 用户退出后提示消费了多少,还剩多少
'''
# dic1 = {'火女':'安妮','火男':'布兰德','维恩':'暗影猎手','剑豪':'哈莎阁'}
# print(dic1.items())
goods = [
{'name':'电脑','price':1999},
{'name':'鼠标','price':10},
{'name':'美女','price':50},
{'name':'游艇','price':20},
{'name':'火箭','price':250},
]
fei_yong = 0
shop_car = {}
money = input("请输入你的金额")
if money.isdigit():
#这是真钱
while 1:
for i in range(len(goods)):
print(i+1,goods[i]["name"],goods[i]["price"])
choose = input("请输入你选择的商品序号:(N. 结算,Q. 退出)")
if choose.isdigit() and 0 < int(choose) < len(goods):
int_index = int(choose)-1
if shop_car.get(int_index) == None:
shop_car[int_index] =1
else:
shop_car[int_index] = shop_car[int_index]
print(shop_car)
elif choose.upper() == 'N':
# 结算
# ... == pass
# 1.2 - 1.0 == 0.2 获取到是False 一个数学算法(牛顿偏离法)导致的
for f in shop_car:
fei_yong = fei_yong + shop_car[f] * goods[f]['price']
if int(money) - fei_yong >= 0:
for k in shop_car:
print(f'您购买的商品是{goods[k]["name"]},单价{goods[k]["price"]},数量{shop_car[k]}')
else:
print('余额不足')
elif choose.upper() == "Q":
print(f'您本次消费{fei_yong},剩余金额{int(money)-fei_yong}')
break
else:
print("你输入有误,请重新输入:")