学习了list,试写一段购物车程序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了学习了list,试写一段购物车程序相关的知识,希望对你有一定的参考价值。
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # Author:Mgin 4 shopping_car = [] 5 salary = int(input("your salary:")) 6 goods_list = [["Watch",20000],["ThinkPad",8500],["IPhone",5800 ],["Suit",3700],["Boost shoes",1700],["Contact Lens",150],["Alex python",80],["Starbucks",30]] 7 print("goods list:") 8 for i in goods_list: 9 print(goods_list.index(i),i) 10 while salary > 0: 11 add = int(input("add goods to your shopping car:")) 12 if add > int(len(goods_list)-1) or add < 0: 13 print("goods have not been found ") 14 stop = input("Do you want to continue:") 15 if stop != ‘n‘: 16 continue 17 else: 18 for i in shopping_car: 19 print(i) 20 print("balance:", salary) 21 break 22 elif salary - goods_list[add][1] > 0: 23 salary = salary - goods_list[add][1] 24 shopping_car.append(goods_list[add]) 25 print("added",goods_list[add][0],"to your shopping car.","\n","balance:",salary) 26 stop = input("Do you want to continue:") 27 if stop != ‘n‘: 28 continue 29 else: 30 for i in shopping_car: 31 print(i) 32 print("balance:",salary) 33 break 34 else: 35 print("you have not enough money") 36 stop = input("Do you want to continue:") 37 if stop != ‘n‘: 38 continue 39 else: 40 for i in shopping_car: 41 print(i) 42 print("balance:", salary) 43 break
程序:购物车
需求:
1.启动程序后,让用户输入工资,然后打印商品列表
2.允许用户根据商品编号购买商品
3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4.可随时退出,退出时,打印已购买商品和余额
以上是关于学习了list,试写一段购物车程序的主要内容,如果未能解决你的问题,请参考以下文章
试写一算法在带头结点的单链表结构上实现线性表操作Length(L)。下面各个步骤的解释要详细
JavaScript学习(八十)—请写一段JS程序提取URL中的各个get参数(参数名和参数个数不确定),将其按key-value形式返回到一个json结构中
我的C语言学习进阶之旅解决 Visual Studio 2019 报错:错误 C4996 ‘fscanf‘: This function or variable may be unsafe.(代码片段