新手之购物车练习

程序:购物车程序
需求:
1.启动程序后,让用户输入工资,然后打印商品列表
2.根据商品编号购买商品
3.用户选择商品后,检索余额是否够,够就直接付款,不够就提醒
4.可以随时退出,退出时,打印已购商品和余额
product_list = [  
    ('iphone', 5800),
    ('mac pro', 9800),
    ('bike', 800),
    ('watch', 1600),
    ('coffee', 31),
    ('alex python', 120),
               ]
# 产品列表,建立产品列表,物品名称/价格

shopping_list = []  # 购物车
while True:  
    salary_money = input("Input You Salary:")  
    if salary_money.isdigit():  # 类型判断,整数为真
        salary_money = int(salary_money)  # 强制转换为int型
        while True:  # 购物循环
            # ①for item in product_list:
            # ①print(product_list.index(item),item) 
            #for循环打印列表内所有在售物品编号及item,方法①
            for index, item in enumerate(product_list):  
      # for循环,方法②enumerate 可列举的将列表中的数据按照(index序号,item内容)形式分别显示
                print(index, item)  
            user_choice = input("what do you want?》》:")
            if user_choice == 'q':
                continue_shopping = input("go on shopping y/n?>>>")
                if continue_shopping == 'n':
                    if len(shopping_list) != 0:  # 判断购物车里有没有东西
                        confirm_buy = input("Are you sure to buy it!:(y/n)")  
                        # 再次确认是否购买
                        if confirm_buy == 'y':  # 确认购买
                            print("------------shopping list")  # 打印物品清单
                            for p in shopping_list:  # for循环,打印购物车列表清单
                                print(p)
                            print("you current balance:\033[31;1m%s\033[0m"%salary_money)  
                            # 打印余额
                            exit()
                        else:  # 不买
                         print("Please re-select the item")
                        continue  
                    else:
                        exit()
                else:
                    continue
            elif user_choice.isdigit():  # 如果用户输入的是数字
                user_choice = int(user_choice)  # 强制转换为int型
                if user_choice < len(product_list) and user_choice >= 0:  
# len函数是自动取列表内容的长度,此处列表长度为5,获取到的值为6,如果输入的编号大于等于0且小于物品总数
                    p_item = product_list[user_choice]  
# 根据用户输入的序号,在列表中将对应的商品信息取出赋值给p_item
                    if p_item[1] <= salary_money:  
# 判断用户选择的商品价格,如果小于等于工资
                        shopping_list.append(p_item)  # 将该商品添加到购物车列表内
                        salary_money -= p_item[1]  # 工资减去物品价格
                        print("ADDed %s into shopping cart,your current balance is \033[31;1m%s\033[0m" % (p_item, salary_money))  # 物品已经加入购物车,以及现在的余额
                    else:
                        print("\033[41;1m你的余额不足,只有%s\033[0m" % salary_money)  # 如果物品价格大于工资,输出余额不足,显示具体余额值
                else:
                 print("product code[%s] is not exist" % user_choice)  
            else:
                print("invalid option")  # 输入的是非数字
    elif salary_money == 'q':  # 输入退出程序
        exit()
    else:
        print("please input your salary again!")
    continue  # 输入非法类型工资,跳出本次循环,执行下一次循环

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值