python购物车作业:
li = [
{'name':'苹果','price':10},
{'name':'葡萄','price':20},
{'name':'芒果','price':30},
]
print('欢迎光临')
money = input('让我看看你的钱')
shopping_car = {};
print(money)
if money.isdigit() and int(money) > 0: #str.isdigit 检测字符串是否只由数字组成
for i,k in enumerate(li): #enumerate 遍历枚举对象
print('序号-{},商品-{},价格-{}'.format(i,k['name'],k['price']))
while 1:
choose = input('请输入商品序号')
if choose.isdigit() and int(choose) < len(li):
num = input('请输入数量')
if num.isdigit():
if int(money) > li[int(choose)]['price']*int(num):
money = int(money) - li[int(choose)]['price']*int(num)
if li[int(choose)]['name'] in shopping_car:
shopping_car[li[int(choose)]['name']] = shopping_car[li[int(choose)]['name']] + int(num)
else:
shopping_car[li[int(choose)]['name']] = int(num)
print('购物车中的商品有{},余额为{}'.format(shopping_car,money))
else:
print('余额不足,请充值')
break;
else:
print('请输入正确的商品序号')
“`