用另一种方式完成购物车的功能实现
#!/usr/bin/python
zijin = input("请输入资金:")
if zijin.isdigit():
zijin = int(zijin)
else:
print("请输入数字!")
exit_flag = False
print("欢迎来到本人的购物小店".center(40,"-"))
shop_list = [
("电脑", 1999),
("鼠标", 800),
("键盘", 299),
("鼠标垫", 100)
]
gouwuche = []
xiaofei = 0
while exit_flag is not True:
print("商品列表如下:".center(44,"-"))
for item in enumerate(shop_list):
index = item[0]
sp_name = item[1][0]
sp_price = item[1][1]
print(index, ".", sp_name, sp_price)
user_choice = input("请输入商品编号进行购买:")
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(shop_list):
sp_item = shop_list[user_choice]
if sp_item[1] <= zijin:
gouwuche.append(sp_item)
zijin -= sp_item[1]
xiaofei += sp_item[1]
print("您成功购买了:[%s] 花费了:[%s] 余额为:[%s]" %(sp_item[0], sp_item[1], zijin))
else:
print("您的金额不足!还差 [%s] 才能购买此商品!" %(sp_item[1] - zijin))
else:
if user_choice == "q" or user_choice == "quick":
print("您本次购买了如下商品:".center(40, "-"))
for g_list in gouwuche:
print(g_list)
print("结束".center(45, "-"))
print("您总共消费了:[%s] 余额为:[%s]" %(xiaofei, zijin))
exit_flag = True