功能要求:
要求用户输入总资产,例如:2000
显示商品列表,让用户根据序号选择商品,加入购物车购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
1 goods = [
2 {"name": "电脑", "price": 1999},
3 {"name": "鼠标", "price": 10},
4 {"name": "游艇", "price": 20},
5 {"name": "美女", "price": 998}
6 ]
7 total = 0
8 flag = True
9 money = input('请输入您的总资产:')
10 while flag:
11 for i in goods:
12 print('{} {} {}'.format(goods.index(i) + 1, i['name'], i['price']))
13 num_of_choose = input('请选择商品的序号(按Q退出,不区分大小写):')
14 if num_of_choose.isdigit():
15 num_of_choose = int(num_of_choose)
16 if num_of_choose > 0 and num_of_choose <= len(goods):
17 total = total + goods[num_of_choose - 1]['price']
18 if total < int(money):
19 print('成功加入购物车!')
20 print('你当前选择的商品为:{} 价格为:{}'.format(goods[num_of_choose - 1]['name'],goods[num_of_choose - 1]['price']))
21 print('总价为:{}'.format(total))
22 else:
23 print('账户余额不足!')
24 flag = False
25 else:print('请输入有效数字!')
26 elif num_of_choose.upper() == 'Q':
27 flag = False
28 else:print('请输入整数!')