ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

01_20200910_购物车程序

2020-09-10 19:32:37  阅读:227  来源: 互联网

标签:salary product 01 list choice item user 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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有