0
点赞
收藏
分享

微信扫一扫

购物车程序

夹胡碰 2022-02-10 阅读 34
python
"""
程序:购物车程序
需求:
1 启动程序后,让用户输入工资,然后打印商品列表
2 允许用户根据商品编号购买商品
3 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4 可随时退出,退出时,打印已购买商品和余额
"""

shop_list = [
    ('iphone', 5800),
    ('mac pro', 9800),
    ('mouse', 500),
    ('bike', 600),
    ('book', 99),
]

shop_car = []

salary = input("请输入您的工资:")
if salary.isdigit():
    salary = int(salary)
    while True:
        for index, shop in enumerate(shop_list):
            print(index, shop)
        user_choice = input("请输入您需要购买的商品序号(输入q退出):")
        if user_choice.isdigit():
            user_choice = int(user_choice)
            if len(shop_list) > user_choice >= 0:
                p_item = shop_list[user_choice]
                if salary > p_item[1]:
                    shop_car.append(p_item)
                    salary -= p_item[1]
                    print(f'您购买的商品为:{shop_car},余额剩余:{salary}')
                else:
                    print(f'您的余额不足,剩余{salary}元')
            else:
                print(f'您输入的商品序号{user_choice}不存在')
        elif user_choice == 'q':
            print(f'您购买的商品为:{shop_car},余额剩余:{salary}')
            exit()
        else:
            print('输入无效,请输入数字')
else:
    print('无效,请输入数字')

举报

相关推荐

0 条评论