作业要示:

购物车程序:
启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表
允许用户根据商品编号购买商品
用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
可随时退出,退出时,打印已购买商品和余额
在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示
用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买
允许查询之前的消费记录

先上一下流程吧!

具体代码还在优化中》》》先一下与流程图对应的代码吧!

 #!usr/bin/env python
#-*-coding:utf-8-*-
# Author calmyan
import time,sys,os
with open('user_file.txt','r',encoding='utf-8') as user_file:
user_list=eval(user_file.readline())#转换用户文件中的字符串
lock_name=open('lock_name.txt','r+',encoding='utf-8')#打开锁定用户列表文件
lock_list=eval(lock_name.readline())#转换锁定用户文件中的字符串 def userchar(_user_name):#检测用户名的函数
while True:
if _user_name in user_list and _user_name not in lock_list:#判断用户是否存在并正常
return True#存在返回真
break
elif _user_name in user_list and _user_name in lock_list:#判断用户是否被锁定
print('您的用户已经被锁定,请联系商家解锁!')
break
else:
print('您输入用户不存在,请先注册')
break def confirm():#退出确认函数
qu=input("是否退出登陆!按\033[31;1mY/y\033[0m退出? 按其它键重新登陆:")
if qu=='Y'or qu=='y':
print("您已经退出!" )
exit()#退出程序
else :
pass def user_pass():
bool=True#定义退出循环条件变量
while bool:
_user_name=(input('请输入用户名:'))
if userchar(_user_name.strip()):#运行检测用户名的函数判断,去除两边空格
count=3#定义锁次数
while count>0 :#如果密码错误次数超过退出密码输入
#password=getpass.getpass('请输入密码:')
password=input('请输入密码:')
if password==user_list[_user_name]:#密码通过验证结束循环
print("密码通过验证")
bool=False
print("欢迎您 \033[32;1m%s\033[0m 您已经登陆成功!" % _user_name)
return _user_name#返回用户名
else:
count -=1#次数减少
print("密码错误请重试,剩余:\033[31;1m%s\033[0m次! "% count)
pass
else:
print("\033[31;1m超出所试次数,账户已锁定!请联系商家!\033[0m")
lock_list.append(_user_name)#将用户名加出锁定列表
lock_name.seek(0)#指针移动到开头,以免产生多个元组
lock_name.write(str(lock_list))#写入锁定用户文件
lock_name.tell()#获取当前指针,以免产生多个元组
lock_name.close()#关闭锁定用户文件
confirm()#退出确认函数
#continue#退回到输入用户名的位置
else:#如果用户名出错新来过
confirm()
pass def infor():#打开用户信息函数
with open('user_infor','r+',encoding='utf-8') as user_infor:#打开用户购物信息文件
infor_list=eval(user_infor.readline())#转换文件的字符串
return infor_list #程序开始:
if os.path.exists('history'):#确认文件是否存在
pass
else:
with open('history','w',encoding='utf-8') as user_history:#创建一个用户帐户购物记录文件
user_history.write('{}')#写入空值
pass if os.path.exists('user_infor'):#确认文件是否存在
pass
else:
with open('user_infor','w',encoding='utf-8') as user_infor:#创建一个用户帐户信息记录文件
user_infor.write('{}')#写入空值
pass print("欢迎来到\033[31;1mpython\033[0m购物商城!")
time_format='%y-%m-%d %X'#定义时间格式
times=time.strftime(time_format)#定义时间
_user_name= user_pass()#将通过验证的用户名赋于变量 if _user_name in infor():#如果用户信息存在记录就跳过
pass
else:
while True:
name_char=input("首次登陆,请输入工资(购物资金):")#用户输入工资(购物资金)
if name_char.isdigit():#检测输入是否为整数
name_char=int(name_char)#转为int
with open('history','r+',encoding='utf-8') as user_history:#打开用户帐户购物记录文件
history_list=eval(user_history.readline())#将文本读为字典
history_list[_user_name]={}#创建用户购物记录
user_history.seek(0)#移到开头
user_history.write(str(history_list))#写入文件
with open('user_infor','r+',encoding='utf-8') as user_infor:
infor_list=eval(user_infor.readline())#转换首登用户文件中的字符串
infor_list[_user_name]={"金额":name_char}
user_infor.seek(0)
user_infor.write(str(infor_list))#写入用户信息
break
else:
print('金额不能为0或负数请正确输入金额!')
continue
#再次登陆直接显示用户信息
infor_list=infor()#获取用户信息字典
name_char=int(infor_list[_user_name]['金额'])#获取余额
with open('history','r',encoding="utf-8") as user_history:#读取用户文件
history_list=eval(user_history.readline())#用户购物信息
shoplist=[]#购物车列表
shop_count=0#购物金额统计
###进入商品列表###
goodsname=[]#定义商品列表
print('用户名:\033[32;1m%s\033[0m:'%_user_name)#输出用户信息
print('您的余额:\033[31;1m%s\033[0m:'%name_char)
history=history_list[_user_name]#用户购物记录信息
print('您的购物记录如下:')
for k,v in history.items():#转为列表进行输出
print(k,v)#输出购物记录
print('加载商品列表:')
#加载动态
for i in range(40):
sys.stdout.write('#')
sys.stdout.flush()
time.sleep(0.05)
print('\n') with open('goodslist.txt','r',encoding='utf-8') as f:#打开商品列表文件
for line in f:
a=line.find(',')#定位分割符的索引进行分割
goodsname.append((line[0:a],int(line[a+1:])))#元组模式追加到列表,价格转为整数 while True:
for index,p_item in enumerate(goodsname):#取出下标输出 商品列表
print(index,p_item)#输出商品列表
user_index=input('请输入商品编号添加你所在购买的商品,按用\033[31;1mq/Q\033[0m退出>>>:')
if user_index.isdigit():#判断是否为整数
user_index=int(user_index)#转为数字
if user_index < len(goodsname) and user_index>=0:#判断所选商品编号在在列表
p_item=goodsname[user_index]#读取下标所在(元素)商品
if p_item[1]<=name_char:#余额是否足够
shoplist.append(p_item)#添加到购物车
name_char-=p_item[1]#扣除金额
shop_count+=p_item[1]#累加购物金额
print("添加 \033[32;1m%s\033[0m 到购物车,价格: \033[32;1m%s\033[0m 元"% p_item)
print("你的购物金额余 \033[31;1m%s\033[0m 元"% name_char)
else:
print("你的购物金额余 \033[41;1m%s\033[0m 元,无法购买该商品!" % name_char)
else:
print('输入错误,该编号 \033[41;1m%s\033[0m 的商品不存在!'%user_index)
elif user_index=='q'or user_index=='Q':
print('你的购物清单:')
for up in shoplist:#打印购物清单
print(up)
print("你的购物总金额为:\033[32;1m%s\033[0m 元!"% shop_count)
print("你的购物金额余: \033[31;1m%s\033[0m 元!" % name_char)
while True:
confirm=input("你是否确定退出购物?\033[31;1m Y/y \033[0m 是 \033[31;1mN/n\033[0m 否:")
if confirm== 'Y'or confirm== 'y' :
infor_list[_user_name]['金额']=name_char#修改用户金额(购物后)
shoplist.append(('总额:',shop_count))#列表加入总额
history_list[_user_name][times+'购物清单:']=shoplist#添加时间:清单
with open('user_infor','w',encoding="utf-8") as user_infor:#写入文件用户信息
user_infor.seek(0)#移到最前
user_infor.write(str(user_list))#写入
with open('history','r+',encoding='utf-8') as user_history:#购物信息写入文件
user_history.seek(0)#移到最前
user_history.write(str(history_list))#写入
exit(print("谢谢!欢迎您再次光临!"))
elif confirm=='N'or confirm== 'n' :
break
else:
print("输入错误!请按提示输入!")
continue
else:
print('\033[41;1m输入商品编号错误,请重新输入!\033[0m')

还着一点点,撸了一天的代码了!!还想要更好!!

完美 主义,伤不起来!!

python第七天-作业[购物车]的更多相关文章

  1. 2003031121——浦娟——Python数据分析第七周作业——MySQL的安装及使用

    项目 要求 课程班级博客链接 20级数据班(本) 作业要求链接 Python第七周作业 博客名称 2003031121--浦娟--Python数据分析第七周作业--MySQL的安装及使用 要求 每道题 ...

  2. C语言学习 第七次作业总结

    C语言学习 第七次作业总结 数组可以分为数组和多下标数组(在传统的国内C语言书本中,将其称为二/多维数组). 数组名称 在之前的课程中,大家应该都有印象,对于int a这样的定义,会为变量 a 声明一 ...

  3. 耿丹CS16-2班第七次作业汇总

    Deadline: 2016-11-27 11:59pm 作业内容 第七次作业总结 01.每次成绩发布,麻烦没交作业的同学(暂定得分为-5的),请及时补交: 02.想不出来可以,代码乱成一团不行,命名 ...

  4. 简学Python第七章__class面向对象高级用法与反射

    Python第七章__class面向对象高级用法与反射 欢迎加入Linux_Python学习群  群号:478616847 目录: Python中关于oop的常用术语 类的特殊方法 元类 反射 一.P ...

  5. python实现简单的循环购物车小功能

    python实现简单的循环购物车小功能 # -*- coding: utf-8 -*- __author__ = 'hujianli' shopping = [ ("iphone6s&quo ...

  6. 初学Python(七)——控制语句

    初学Python(七)——控制语句 初学Python,主要整理一些学习到的知识点,这次是控制语句. if : #-*- coding:utf-8 -*- age=raw_input('input yo ...

  7. 网络1711班 C语言第七次作业批改总结

    网络1711班 C语言第七次作业批改总结 1.本次作业评分细则 1.1 基本要求(1分) 按时交 - 有分 未交 - 0分 迟交一周以上 - 倒扣本次作业分数 抄袭 - 0分 泛泛而谈(最多七分) 1 ...

  8. 2017-2018-1 我爱学Java 第六七周 作业

    团队六七周作业 完善版需求规格说明书 制定团队编码规范 数据库设计 后端架构设计 TODOList 参考资料 完善版需求规格说明书 <需求规格说明书>初稿不足之处: 1.开发工具写错 2. ...

  9. Python第七天 函数 函数参数 函数里的变量 函数返回值 多类型传值 函数递归调用 匿名函数 内置函数

    Python第七天   函数  函数参数   函数里的变量   函数返回值  多类型传值     函数递归调用   匿名函数   内置函数 目录 Pycharm使用技巧(转载) Python第一天   ...

随机推荐

  1. SVN 分支主干的相互合并

    1.主干合并到分支 1在本地trunk中先update一下,有冲突的解决冲突,保证trunk和repository已经完全同步, 2.在/branches /MyProject上右键,依次选择”Tor ...

  2. [EXP]Jenkins 2.150.2 - Remote Command Execution (Metasploit)

    ## # This module requires Metasploit: https://metasploit.com/download # Current source: https://gith ...

  3. python之内置装饰器(property/staticmethod/classmethod)

    python内置了property.staticmethod.classmethod三个装饰器,有时候我们也会用到,这里简单说明下 1.property 作用:顾名思义把函数装饰成属性 一般我们调用类 ...

  4. 可以用软连接的方式解决linux内存空间不足的问题

    突然提示说/var空间满了,然后接着系统卡死,最后彻底没辙,重启试试,没想到提示什么系统错误,请联系管理员之类的提示语,也进不去登陆界面啥了.之后用其他电脑连接SSH用root账号访问. # cd / ...

  5. webpack-loader是怎样炼成的

    目录 啰嗦两句 loader 是干什么的 loader 的工具箱 --context loader 实战 啰嗦两句 学习这件事从学习动机上来看,可以分成两种情况:主动学习和被动学习.主动学习就是,某天 ...

  6. 以ActiveMQ为例JAVA消息中间件学习【2】

    前言 之前我们学习了什么是消息中间件,以ActiveMQ为例做了一个最简单的消息中间件的实现.但是我们做的就只能算是个例子而已,因为在实际的项目中肯定会有spring插一脚,所以spring肯定有来管 ...

  7. linux装tomcat遇到的坑

    最开始通过apt-get安装,各种毛病 然后下载tar.gz压缩包解压使用,运行startup.sh可以启动,但是看日志发现 Tomcat启动时卡在 INFO HostConfig.deployDir ...

  8. C# DataGrid 用法---极速入门测试

    目标: 新手编程,只求DataGrid能运行起来,更多功能留在后面探讨. 步骤: 1.新建WPF文档 插入DataGrid控件. <Window x:Class="OASevl.Mai ...

  9. httpd配置文件httpd.conf规则说明和一些基本指令

    apache httpd系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html 本文主要介绍的是httpd的配置文件,包括一些最基本的指令.配置规 ...

  10. [转]ui-grid User can't select the row by clicking the select checkbox available in the respective row when enableFullRowSelection : true"

    本文转自:https://github.com/angular-ui/ui-grid/issues/5239 Try this style to enable checkbox selection: ...