码迷,mamicode.com
首页 > 编程语言 > 详细

2.python之购物车程序

时间:2021-01-01 12:52:57      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:ace   mil   space   ==   exit   turn   int   col   for   

请选择你要购买的商品序号(按q退出):3
你已购买[‘iphone‘, ‘xiaomi‘],总共花费2998,当前可用余额:17002
商品列表如下:
1-[‘iphone‘, 999]
2-[‘lv bag‘, 666]
3-[‘xiaomi‘, 1999]
4-[‘honor‘, 2999]
5-[‘oppo‘, 2999]

 


def shop_1(_salary,lists,allpay):
x=int(_num)
if int(_salary) < int(lists[x][1]):
print(f"对不起,你的余额不足,无法购买此商品,当前可用余额为{_salary}")
return _salary, lists, allpay
else:
_salary = int(_salary) - int(lists[x][1])
shoplist.append(lists[x][0])
allpay += int(lists[x][1])
print(f"你已购买{shoplist},总共花费{allpay},当前可用余额:{_salary}")
return _salary,lists,allpay


lists = [[‘iphone‘,999],[‘lv bag‘,666],[‘xiaomi‘,1999,],[‘honor‘,2999],[‘oppo‘,2999]]
_salary = input("please input your salary:")
allpay = 0
shoplist = []
while True:
print("商品列表如下:")
# for list in lists:
# print(f"{lists.index(list)}-{list}")
for index,list in enumerate(lists):
print(f"{index} - {list}")
_num = input("请选择你要购买的商品序号(q退出)")
if _num.isdigit():
_num = int(_num)
if _num < len(lists) and _num >= 0:
choice = lists.index(lists[_num])
if _num == choice:
(_salary, lists, allpay) = shop_1(_salary, lists, allpay)
else :
print("你输入的序号不存在,请重新输入:")
else:
if _num == ‘q‘:
print("欢迎下次光临!")
print(f"你已购买{shoplist},总共花费{allpay},当前可用余额:{_salary}")
exit()
else:
print("请选择正确的商品序号。")
 

 

2.python之购物车程序

标签:ace   mil   space   ==   exit   turn   int   col   for   

原文地址:https://www.cnblogs.com/baozimantou/p/14201787.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!