20201231-1 购物车程序练习实例
2021/5/31 1:20:10
本文主要是介绍20201231-1 购物车程序练习实例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
程序讲解: 商品列表是动态的,可能随时增加,减少,所以不能写死 Step1:打印商品列表 方式1 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 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")
这篇关于20201231-1 购物车程序练习实例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)