1、程序:购物车程序 需求: 启动程序后,让用户输入工资,然后打印商品列表 允许用户根据商品编号购买商品 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 可随时
1、程序:购物车程序
需求:
- 启动程序后,让用户输入工资,然后打印商品列表
- 允许用户根据商品编号购买商品
- 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
- 可随时退出,退出时,打印已购买商品余额
# -*- coding:utf-8 -*-
# Author:young
#date 20220331
#1、变量、列表等定义
shop_list = [ #定义存放商品的列表
('Watch',3200),
('bike',800),
('booke',80),
('ipad',4800)
]
shopped_list = [] #临时存放商品的购物车
flag = not False
#2、程序主体
salary = input("请输入你的薪水:") #薪水输入
if salary.isdigit(): #判断是否是数字
salary = int(salary) #数字str类型,装换成int类型
while flag: #进入循环中
for i,j in enumerate(shop_list): #列表通过枚举法讲索引打印出来
print(i+1,j)
user_choice = input("请选择商品编码:") #选择商品编码
if user_choice.isdigit():
user_choice = int(user_choice) #str数字转换成int
if user_choice >= 1 and user_choice <= len(shop_list): #判断列表长度
if salary >= shop_list[user_choice -1][1]: #薪资和商品价格对比
salary = salary - shop_list[user_choice -1][1]
shopped_list.append(shop_list[user_choice -1]) #商品添加到购物车
else:
print("你的钱不够购买此商品,请选择其他商品或者退出购买!")
else:
print("你输入的商品编码有误,没有此商品!")
elif user_choice == 'q':
flag = False
print(f"你购买了这些商品{shopped_list},还剩余{salary}元钱")
else:
print("你输入的商品编码必须是数字哦!")
else:
print("你的薪水输入有误,必须是数字哦!")