题目:

购物车程序
salary = 5000
1. iphone6s 5800
2. mac book 9000
3. coffee 32
4. python book 80
5. bicyle 1500
>>>:1
余额不足,-3000
>>>:5
已加入bicyle 到你的购物车, 当前余额:3500
>>>:quit
您已购买一下商品
bicyle 1500
coffee 30
您的余额为:2970
欢迎下次光临

程序如下:

# Author:Administrator
# date: // Salary =
merchandise_list = (['iphone6s', ], ['mac book', ], ['coffee', ], ['python book', ], ['bicycle', ])
shopping_list = []
while True:
user_input = input("请输入:")
if user_input == "quit":
if shopping_list.__len__() > :
print(merchandise_list)
print("您购买以下商品:")
for item in shopping_list:
item = list(item)
print("商品:%s 价格:%d 数量 %d" % (item[], item[], item[]))
exit("您的余额为:%d,欢迎下次光临!" % Salary)
elif user_input.isdigit():
Type = int(user_input)
if < Type < :
merchandise = merchandise_list[Type - ]
if Salary < merchandise[]:
print("余额不足:%d" % (Salary - int(merchandise[])))
else:
Salary -= int(merchandise[])
for item in shopping_list:
list_item = list(item)
if list_item[] == merchandise[]:
list_item[] +=
shopping_list.remove(item)
shopping_list.append(tuple(list_item))
print("商品%s购买成功" % item[])
break
else:
shopping_item = []
shopping_item.extend(merchandise)
shopping_item.extend([])
print("商品%s购买成功" % shopping_item[])
shopping_list.append(tuple(shopping_item))
else:
print("没有您购买的商品,请输入[1-5]")
else:
print("请输入[1-5]或quit")
04-13 20:31