购物车
Posted xhwy-1234
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了购物车相关的知识,希望对你有一定的参考价值。
@time 2019/12/17 15:18
"""
需求:
1、启动程序后,让用户输入工资,然后打印商品列表
2、允许用户根据商品编号购买商品
3、用户购买商品后,检测用户余额是否足够,够就直接扣款,不够就提醒
4、可随时退出,退出时,打印已购商品和余额
"""
# 1、第一步:创建商品列表,以及空的购物车列表以便接收商品
shopping_list = [("程咬金", 20), ("后羿", 40), ("李元芳", 50), ("王昭君", 80)]
buy_list = []
# 2、第二步:判断工资输入的正确性,以及转为int类型,以便下边代码比较时使用
salary = input("请输入您的工资:")
if salary.isdigit():
salary = int(salary)
else:
print("您是外星来的吗,请正确输入工资呢")
exit()
# 3、创建死循环
while True:
# 打印商品
for shopping in shopping_list:
print(shopping_list.index(shopping), shopping[:])
# 上述两行代码可以使用:for index, shopping in enumerate(shopping_list):
# print(index, shopping)
shopping_number = input("请输入您要购买的商品编号:")
# 判断
if shopping_number.isdigit():
shopping_number = int(shopping_number)
if len(shopping_list) >= shopping_number >= 0:
p_shopping = shopping_list[shopping_number]
if p_shopping[1] <= salary:
buy_list.append(shopping_list[int(shopping_number)])
salary -= p_shopping[1]
print("您已经购买%s,您的余额还有 33[32;1m%d 33[0m" % (shopping_list[int(shopping_number)][0], salary))
else:
print("