编程语言
首页 > 编程语言> > 01_20200910_购物车程序

01_20200910_购物车程序

作者:互联网

  # 商品列表是动态的,可能随时增加,减少,所以不能写死
Step1:打印商品列表 方式1 product_list = [     ('Iphone',5800),     ('Mac Pro',9800),     ('Bike',800),     ('Watch',10600),     ('Coffee',31),     ('Alex Python',120), ]   Link01: https://www.cnblogs.com/azxsdcv/p/13647709.html
工资只输入一次,所以放循环外面 salary = input("Input your salary: ") if salary.isdigit(): Link02:      salary = int(salary)     while True:         for item in product_list:             print(product_list.index(item), item)
        break
方式2 不通过下标,一个更直接的方法 product_list = [     ('Iphone',5800),     ('Mac Pro',9800),     ('Bike',800),     ('Watch',10600),     ('Coffee',31),     ('Alex Python',120), ]
salary = input("Input your salary: ") if salary.isdigit():     salary = int(salary)     while True:         for index, item in enumerate(product_list):             print(index, item)
        break 1. 关于 enumerate >>> a = [1,2,3] >>> for i in enumerate(a):print(i) ... (0, 1) (1, 2) (2, 3) 每一个 i 都变成了 一个元组 第一个是小标,第二个是数据本身 enumerate 做的就是 取出了 列表的下标
Step2:根据用户选择做判断 product_list = [     ('Iphone',5800),     ('Mac Pro',9800),     ('Bike',800),     ('Watch',10600),     ('Coffee',31),     ('Alex Python',120), ] shopping_list = [] salary = input("Input your salary: ") if salary.isdigit():     salary = int(salary)     while True:         for index, item in enumerate(product_list):             print(index, item)         user_choice = input("选择要买嘛?>>>:")         if user_choice.isdigit():             user_choice = int(user_choice)             if user_choice < len(product_list) and user_choice >= 0:                 # 接下来判断钱是否够用                 p_item = product_list[user_choice]                 if p_item[1] <= salary:    # 买的起                     shopping_list.append(p_item)                     salary -= p_item[1]                     print("Added %s into shopping cart, your current balance is %s" %(p_item, salary))         elif user_choice == 'q':             print('exit....')         else:             print("invalid option") 1. 如果想给结果变个颜色,红色 31,绿色 32 print("Added %s into shopping cart, your current balance is \033[31;1m%s\033[0m" %(p_item, salary)) 这个就是死记,后面 \033[0m 一定要关闭 product_list = [     ('Iphone',5800),     ('Mac Pro',9800),     ('Bike',800),     ('Watch',10600),     ('Coffee',31),     ('Alex Python',120), ] shopping_list = [] salary = input("Input your salary: ") if salary.isdigit():     salary = int(salary)     while True:         for index, item in enumerate(product_list):             print(index, item)         user_choice = input("选择要买嘛?>>>:")         if user_choice.isdigit():             user_choice = int(user_choice)             if user_choice < len(product_list) and user_choice >= 0:                 # 接下来判断钱是否够用                 p_item = product_list[user_choice]                 if p_item[1] <= salary:    # 买的起                     shopping_list.append(p_item)                     salary -= p_item[1]                     print("Added %s into shopping cart, your current balance is \033[31;1m%s\033[0m" %(p_item, salary))         elif user_choice == 'q':             print('exit....')         else:             print("invalid option") 2. 这是能买得起的情况,如果买不起呢? product_list = [     ('Iphone',5800),     ('Mac Pro',9800),     ('Bike',800),     ('Watch',10600),     ('Coffee',31),     ('Alex Python',120), ] shopping_list = [] salary = input("Input your salary: ") if salary.isdigit():     salary = int(salary)     while True:         for index, item in enumerate(product_list):             print(index, item)         user_choice = input("选择要买嘛?>>>:")         if user_choice.isdigit():             user_choice = int(user_choice)             if user_choice < len(product_list) and user_choice >= 0:                 # 接下来判断钱是否够用                 p_item = product_list[user_choice]                 if p_item[1] <= salary:    # 买的起                     shopping_list.append(p_item)                     salary -= p_item[1]                     print("Added %s into shopping cart, your current balance is \033[31;1m%s\033[0m" %(p_item, salary))                 else:                     print("\033[41;1m你的余额只剩[%s]啦,还买个毛线\033[0m"%salary)                     2-1 31 和 41 的区别                     32 是绿色,42 就是背景是绿色         elif user_choice == 'q':             print('exit....')         else:             print("invalid option")

3. 如果现在想退出了,打印已经买的产品 product_list = [     ('Iphone',5800),     ('Mac Pro',9800),     ('Bike',800),     ('Watch',10600),     ('Coffee',31),     ('Alex Python',120), ] shopping_list = [] salary = input("Input your salary: ") if salary.isdigit():     salary = int(salary)     while True:         for index, item in enumerate(product_list):             print(index, item)         user_choice = input("选择要买嘛?>>>:")         if user_choice.isdigit():             user_choice = int(user_choice)             if user_choice < len(product_list) and user_choice >= 0:                 # 接下来判断钱是否够用                 p_item = product_list[user_choice]                 if p_item[1] <= salary:    # 买的起                     shopping_list.append(p_item)                     salary -= p_item[1]                     print("Added %s into shopping cart, your current balance is \033[31;1m%s\033[0m" %(p_item, salary))                 else:                     print("\033[41;1m你的余额只剩[%s]啦,还买个毛线\033[0m"%salary)         elif user_choice == 'q':             print("-------shopping list--------")             for p in shopping_list:                 print(p)             3-1. 最后是退出,用exit()             print("Your current balance:",salary)             exit()         else:             print("invalid option")
4. 现在可以输入 6 应该如何处理? product_list = [     ('Iphone',5800),     ('Mac Pro',9800),     ('Bike',800),     ('Watch',10600),     ('Coffee',31),     ('Alex Python',120), ] shopping_list = [] salary = input("Input your salary: ")                   # 输入工资,判断是不是数字 if salary.isdigit():                                    # 如果是数字 # 如果输入的工资不是数字,就退出了,可以加 else 判断     salary = int(salary)                                # 将其int     while True:         for index, item in enumerate(product_list):             print(index, item)         user_choice = input("选择要买嘛?>>>:")         if user_choice.isdigit():             user_choice = int(user_choice)
            if user_choice < len(product_list) and user_choice >= 0:                 # 接下来判断钱是否够用                 p_item = product_list[user_choice]                 if p_item[1] <= salary:    # 买的起                     shopping_list.append(p_item)                     salary -= p_item[1]                     print("Added %s into shopping cart, your current balance is \033[31;1m%s\033[0m" %(p_item, salary))                 else:                     print("\033[41;1m你的余额只剩[%s]啦,还买个毛线\033[0m"%salary)             # 4-1 如果输入的数字不在范围内             else:                 print("product code [%s] is not exist!"% user_choice)
        elif user_choice == 'q':             print("-------shopping list--------")             for p in shopping_list:                 print(p)             print("Your current balance:",salary)             exit()         else:             print("invalid option")

标签:salary,product,01,list,choice,item,user,20200910,购物车
来源: https://www.cnblogs.com/azxsdcv/p/13647726.html