标签:代码 app 死循环 购物 循环 类型 pre lis 打印
"""
需求:
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,您的余额还有\033[32;1m%d\033[0m" % (shopping_list[int(shopping_number)][0], salary))
else:
print("\033[31;1m您的余额不足,您的余额是%s\033[0m" % salary)
else:
print("您真调皮,商品柜中没有该商品呢")
elif shopping_number == "q":
print("您购买的商品有", buy_list)
exit()
else:
print("您真调皮,商品柜中没有该商品呢")
标签:代码 app 死循环 购物 循环 类型 pre lis 打印
原文地址:https://www.cnblogs.com/xhwy-1234/p/12079114.html