第二周作业购物车程序
#coding:utf-8 #Author:Mr Zhi file_open = open('购物车用户信息','r+',encoding="utf-8") #购物车用户信息文件保存着用户名密码和余额 f = str(file_open.read()) for line in f:file_str = str(f) file_open_dict = eval(file_str) #转换成字典(文件内容原先是字典) username = input("输入用户名:") password = input("输入密码:") while True:if username in file_open_dict: #判断用户名是否在购物车用户信息文件中if password in file_open_dict[username]:salary = int(file_open_dict[username][password])print('''\033[32;1m欢迎登录,当前余额为%s\033[0m''' % salary)breakelse:password = input("密码错误,请重新输入密码")continueelse:password_salary = {} #定义密码,工资空字典salary_str = input("欢迎第一次登陆,请输入工资:")salary =float(salary_str) #输入的工资转成数字password_salary[password] = salary #密码与数字对应file_open_dict[username] = password_salary #用户名和密码工资对应file_open.seek(0) #文件读取到开头file_open.write(str(file_open_dict)) #把用户名密码和工资写到文件中file_open.tell() #返回当前位置break product_list = [ #购物清单['iphone6', 5000],['bike', 800],['python books', 200],['bag', 300],['macbook pro', 9000], ] history_f = open('history','r+',encoding="utf-8") #打开history文件 f2 = str(history_f.read()) for line in f2:file_str2 = str(f2) history_line = eval(file_str2) if username not in history_line:history_line[username] = [] shoppinglist = history_line[username] shoppinglist_new = [] choice = input ("\033[35;1m是否需要查询历史购物记录(y/n)\033[0m") #询问是否查询历史记录 if choice == "y" or choice =="Y":print("---历史购物记录---")print(shoppinglist) while True:print("---商品清单---")for index,item in enumerate(product_list): #对元组进行遍历并加上索引print(index,item) #输出商品choice = input("输入商品编码:")#choice = int(choice)if choice.isdigit():choice = int(choice)if choice < len(product_list) and choice >= 0:p_item = product_list[int(choice)] #把商品赋值给p_itemprint(p_item)if p_item[1] <= salary: #判断商品价格和工资的大小shoppinglist_new.append(p_item) #把商品加到元组salary -= p_item[1] #余额 = 工资 - 商品print("\033[37;1m买了 %s 还剩下 %s 元\033[0m" % (p_item, salary))else:print("你的余额不足")else:print("你选择的编码%无效" % choice)elif choice == "q" or choice =="Q":file_open_dict[username][password] = salary #工资给对应的用户密码位置 file_open.seek(0)file_open.write(str(file_open_dict)) #把余额写到购物车用户信息文件中 file_open.tell()print("----已购商品清单----")print(shoppinglist_new) # 打印清单print("\033[31;1m你的余额:%s\033[0m" % salary) # 打印余额shoppinglist.extend(shoppinglist_new) # 本次购物记录追加到购物列表中history_line[username] = shoppinglist # 购物列表和用户名对应history_f.seek(0) #移动文件读取指针到开头history_f.write(str(history_line)) # 购物记录写入文件history_f.tell() #返回文件的当前位置breakelse:print("你选择的编码%无效" % choice)
转载于:https://www.cnblogs.com/wenwei-blog/p/6872686.html
第二周作业购物车程序相关推荐
- 软件工程 第二周作业
##软件工程第二周作业 提出问题 1. 一般来说,想要自己的程序跑得又快又好,就要减少函数的反复调用,但有所得则必有所失,效能提高就有可能伴随着程序的稳定性的降低,这两者应该如何权衡呢? 2. 关于5 ...
- 2017-2018-1 20179215《Linux内核原理与分析》第二周作业
20179215<Linux内核原理与分析>第二周作业 这一周主要了解了计算机是如何工作的,包括现在存储程序计算机的工作模型.X86汇编指令包括几种内存地址的寻址方式和push.pop.c ...
- 「数据结构」普林斯顿算法课第二周作业
「数据结构」普林斯顿算法课第二周作业 Algorithm I, Princeton 编程作业: Deques and Randomized Queues 思路 Deque.java Randomize ...
- OUC2022秋季软件工程第16组第二周作业
OUC2022秋季软件工程第16组第二周作业 目录 文章目录 OUC2022秋季软件工程第16组第二周作业 目录 一.视频学习心得及问题总结 1.学习心得 张欣悦: 赵艳蕊: 杨婷: 郑欣欣: 张维娜 ...
- 软件质量与测试--第二周作业 WordCount
软件质量与测试--第二周作业 WordCount Github地址: https://github.com/RicardoDZX/WordCount PSP: PSP2.1 PSP 阶段 预估耗时 ( ...
- 数据压缩第二周作业——图片TIFF格式分析
数据压缩第二周作业 TIFF格式概述 特点 组成 图像文件头(Image File Header - IFH) Byteorder Version Offest to first IFD 图像文件目录 ...
- 【中文】【吴恩达课后编程作业】Course 5 - 序列模型 - 第二周作业 - 词向量的运算与Emoji生成器
[中文][吴恩达课后编程作业]Course 5 - 序列模型 - 第二周作业 - 词向量的运算与Emoji生成器 上一篇:[课程5 - 第二周测验]※※※※※ [回到目录]※※※※※下一篇:[课程5 ...
- 2019年春季学期第二周作业(文件指针)
2019年春季学期第二周作业(基础作业) 请在第一周作业的基础上,继续完成:找出给定的文件中数组的最大值及其对应的最小下标(下标从0开始).并将最大值和对应的最小下标数值写入文件. 输入: 请建立以自 ...
- 学习linux第二周作业
第二周作业: 本周作业内容: 1.Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示. touch,rm,mv,cp,file,ls,chmod,chown,ln,rename, ...
- 序列模型第二周作业1:Operations on word vectors
来自吴恩达深度学习系列视频:序列模型第二周作业1:Operations on word vectors.如果英文对你来说有困难,可以参照:[中文][吴恩达课后编程作业]Course 5 - 序列模型 ...
最新文章
- 如果是能简单解决的问题,就不用想得太复杂了
- net-speeder 安装
- Excel VBA中的等价(Eqv)和蕴含(Imp)
- Redis:事务、管道、Lua脚本
- Android studio导入support-v4.jar
- Java之数据库基础理论
- Webtrends收购实时分析公司Reinvigorate
- ios开发快速入门教程
- Windows下配置Python环境报错error: Microsoft Visual C++ 14.0 or greater is required. Get it with “Microsoft
- 计算机开机会跳过自检嘛,电脑开机怎样跳过自检
- YOLO系列(v1~v3)的学习及YOLO-Fastest在海思平台的部署(下)
- 512-rear chassis fan not detected的解决方法
- Typo: In word xxxx
- 计算机二级买那几本书,计算机二级公共基础知识买哪本书复习好?重点在哪..._公共英语考试_帮考网...
- [小说连载]张小庆,在路上(30)-保持希望
- win 10 mmdetection 配置
- 键盘的 SysRq按键使用详解
- 二进制(二):十进制转二进制的两种方法
- 远程控制软件如何实现两台电脑连接
- HTML 锚点的作用及用法
热门文章
- python 怎么把 写好的代码 放到服务器执行_给 Python 初学者解答了无数遍的 Python 环境相关问题...
- centos7 java 1.8_Centos7下安装Java JDK 1.8
- jquery name选择器_【百战程序员从开始到植发】之jquery
- 【UVA725】Division(暴力求解--水题)
- hdoj1176:免费馅饼(dp基础题-数塔思想)
- keras分类器模型
- SLAM--G2o实现BA优化
- 算法:Path Sum(路径总和)
- 12月21诛仙服务器维护,1月21日全服停机更新维护公告
- Criteo数据集探索