'''
用函数完成登录注册以及购物车的功能
作业需求:
1,启动程序,用户可选择四个选项:登录,注册,购物,退出。
2,用户注册,用户名不能重复,注册成功之后,用户名密码记录到文件中。
3,用户登录,用户名密码从文件中读取,进行三次验证,验证不成功则退出整个程序。
4,用户登录成功之后才能选择购物功能进行购物,购物功能(就是将购物车封装到购物的函数中)。
5,退出则是退出整个程序。
'''
'''
分析:将用户信息保存到文件userinfo.txt(不考虑多个账号同时使用)
存储格式为{"alex":{"pwd":"123456", "status":"logged", "money": 8000, "shopping":{}},
"wusir":{"pwd": "123456", "status":"logout","money": 6000, "shopping":{}},
......
}
'''
#判断文件是否存在,不存在,则创建空文件
def mk_file():
if not os.path.exists(filename):
f = open(filename, mode="w", encoding="utf-8")
f.close() #获取文件中的内容,并将内容转换为dict类型
def get_file_content():
usr_dic = {}
with open(filename, mode="r", encoding="utf-8") as f:
content = f.read().strip()
if content != "":
usr_dic = eval(content)
return usr_dic #修改文件内容
def set_file_content(usr_dic):
with open("%s.bak" % filename, mode="w", encoding="utf-8") as f:
f.write(str(usr_dic))
os.remove(filename)
os.rename("%s.bak" % filename, filename) #用户注册
def register():
while 1:
usr = input("Register Username: ").strip()
pwd = input("Register Password: ").strip()
if usr != "" and pwd != "": # 判断用户名和密码是否合法
if len(usr) < 3 or len(usr) > 30:
print("\033[31;0m用户名的长度必须在3~30.\033[0m")
continue if len(pwd) < 6 or len(pwd) > 20:
print("\033[31;0m密码的长度必须在6~20.\033[0m")
continue mk_file() # 判断文件是否存在,不存在直接创建空文件
usr_dic = get_file_content() #读文件,判断用户是否存在,存在则提醒已存在,不存在则直接添加
if usr in usr_dic:
print("\033[31;0m注册失败, 用户名: %s已存在!\033[0m" % usr)
return
else:
usr_dic[usr] = {"pwd": pwd}
set_file_content(usr_dic)
print("\033[32;0m恭喜您,用户名: %s注册成功!\033[0m" % usr)
return True
else:
print("\033[31;0m用户名和密码不能为空!\033[0m") #用户登录
def login():
while 1:
usr = input("Login Username: ").strip()
pwd = input("Login Password: ").strip()
if usr != "" and pwd != "": ## 判断用户名和密码是否为空
mk_file() #判断文件是否存在,不存在则创建空文件
usr_dic = get_file_content() #读文件,判断用户名和密码是否正确
if usr in usr_dic:
if usr_dic[usr]["pwd"] == pwd: #登录成功
return usr
#用户密或密码错误
return
else:
print("\033[31;0m用户名和密码不能为空.\033[0m") #记录用户信息
def record_userinfo(usr, stat="logged", money=0, shopping={}):
usr_dic = get_file_content()
usr_dic[usr]["status"] = stat
usr_dic[usr]["money"] = usr_dic[usr].get("money", 0) + money
#更新用户已购买的商品信息
if shopping != {}:
sp_dic = usr_dic[usr].get("shopping", {})
new_dic = shopping
for k, v in sp_dic.items():
if k in shopping:
v[2] += shopping[k][2]
new_dic[k] = v
usr_dic[usr]["shopping"] = new_dic
set_file_content(str(usr_dic)) #购物功能
def shopping(usr):
shopping_lst = [("电脑", 2999), ("鼠标", 30), ("键盘", 80), ("音响", 888), ("耳机", 60)]
shopping_carts = {} print("\033[32;0m欢迎来到淘宝电脑城\033[0m".center(50, "*"))
while 1:
money = input("请输入您要充值的金额(元): ").strip()
if money.isdigit():
money = int(money)
usr_dic = get_file_content().get(usr, {}) #获取账号余额
money_total = usr_dic.get("money", 0) + money
print("\033[32;0m恭喜您,成功充值%d元! 您的账号余额为%d元.\033[0m" % (money, money_total))
record_userinfo(usr, money=money) #记录用户充值金额
money = money_total
break
else:
print("输入有误,请重新输入!") while 1:
#显示商品
print("\033[32;0m商品列表\033[0m".center(50, "*"))
print("编号\t商品名称\t商品价格")
for i, v in enumerate(shopping_lst, 1):
print("%d\t\t%s\t\t%s" % (i, v[0], v[1]))
print("n\t\t商品结算")
print("q\t\t退出") #用户选择商品
choice = input("请输入您要购买的商品编号: ").strip()
if choice.isdigit():
choice = int(choice)
if choice >= 1 and choice <= len(shopping_lst):
sp_name = shopping_lst[choice-1][0]
sp_price = shopping_lst[choice-1][1]
if choice in shopping_carts: #判断商品是否已在购物车
shopping_carts[choice][2] += 1
else:
shopping_carts[choice] = [sp_name, sp_price, 1]
print("\033[32;0m恭喜您,成功将商品: %s添加购物车!\033[0m" % sp_name)
else:
print("\033[31;0m商品不存在\033[0m")
elif choice.upper() == "N":
while 1:
if shopping_carts == {}: #判断购物车是否为空
print("\033[33;0m购物车空空如也!请先选择您要购买的商品,再来结算吧.\033[0m")
break print("\033[32;0m购物车商品信息\033[0m".center(50, "*")) #显示购物车商品
print("序号\t商品名称\t商品单价\t商品数量")
consume_total = 0
for i in shopping_carts:
goods = shopping_carts[i]
print("%s\t\t%s\t\t%s\t\t\t%s" % (i, goods[0], goods[1], goods[2]))
consume_total += goods[1] * goods[2] #钱够,直接购买商品
if money - consume_total >= 0:
money -= consume_total
print("\033[32;0m恭喜您,商品结算成功,本次消费共: %d元, 账号余额为%d元.\033[0m" % (consume_total, money))
record_userinfo(usr, shopping=shopping_carts, money=-consume_total) #更新用户余额和已购买商品信息
return
#余额不足
else:
print("\033[31;0m商品总金额: %d元, 您还差 %d元.\033[0m" % (consume_total, consume_total-money))
del_choice = input("请选择您要删除的商品序号: ").strip() #移除购物车商品
if del_choice.isdigit():
del_choice = int(del_choice)
if del_choice in shopping_carts: #判断用户输入的商品序列号是否在购物车中,若在,直接移除
shopping_carts[del_choice][2] -= 1
if shopping_carts[del_choice][2] == 0:
del shopping_carts[del_choice]
else:
print("\033[31;0m输入有误,请重新输入!\033[0m")
else:
print("\033[31;0m输入有误,请重新输入!\033[0m")
elif choice.upper() == "Q":
break
else:
print("\033[31;0m商品不存在!\033[0m") import os
choice_lst = ["登录", "注册", "购物", "退出"]
print("\033[32;0m欢迎来到淘宝购物城\033[0m".center(50, "*"))
filename = "userinfo.txt"
usr = ""
Flag = 1 while Flag:
#显示4个选项
for i in choice_lst:
print(i)
choice = input("请输入你要做的操作: ").strip()
# 判断用户输入的选项是否正确
if choice in choice_lst:
if choice == "登录":
#可以进行三次验证
for i in range(2, -1, -1):
usr = login()
if usr:
print("\033[32;0m恭喜您,账号: %s登录成功!\033[0m" % usr)
#登录成功后,记录登录状态
record_userinfo(usr)
break
else:
if i == 0: #进行三次验证,验证不成功则退出整个程序
print("\033[31;0m用户名或密码错误,您的账号已被锁住.\033[0m")
Flag = 0
break
else:
print("\033[31;0m用户名或密码错误,您还有%d次机会.\033[0m" % i)
elif choice == "注册":
register()
elif choice == "购物":
usr_dic = get_file_content().get(usr, {})
if usr_dic == {}:
print("\033[31;0m请先登录再购物!\033[0m")
else:
if usr_dic.get("status", 0) != "logged":
print("\033[31;0m请先登录再购物!\033[0m")
else:
shopping(usr)
elif choice == "退出":
print("\033[32;0m欢迎下次光临\033[0m".center(50, "*"))
#修改用户的登录状态
if usr != "":
record_userinfo(usr, stat="logout")
break
else:
print("\033[31;0m输入错误, 请重新输入!\033[0m")

python3 练习题(用函数完成登录注册以及购物车的功能)的更多相关文章

  1. python3 使用random函数批量产生注册邮箱

    '''你是一个高级测试工程师,现在要做性能测试,需要你写一个函数,批量生成一些注册使用的账号. 1.产生的账号是以@163.com结尾,长度由用户输,产生多少条也由用户输入,2.用户名不能重复,用户名 ...

  2. Django项目登录注册系统

    Django项目之个人网站 关注公众号"轻松学编程"了解更多. Github地址:https://github.com/liangdongchang/MyWeb.git 感兴趣的可 ...

  3. android安卓Sqlite数据库实现用户登录注册

    看了很多别人写的安卓SQlite数据的操作代码,一点也不通俗易懂,我觉得我写的不错,而且安卓项目也用上了,所以在博客园里保存分享一下!建立一个类 并继承SQLiteOpenHelper public ...

  4. 7. Swift 基于Xmpp和openfire实现一个简单的登录注册

    1. 基本步骤:首先导入Xmpp框架,配置环境 ->由于我们使用的是OC的Xmpp框架,再进行Swift开发时需要进行桥接. 具体方法就是创建一个基于c的.h的头文件,然后将我们需要编译OC的语 ...

  5. 【Salvation】——登录注册存储数据&验证用户

    写在前面:登录注册功能是在纯Unity3D环境内实现的,用到UGUI绘制界面技术,数据库的部分是后面拓展加进来的,这里数据存储是指存在XML用户文件中. 注册用户名和密码 zc() 用户名和密码登录 ...

  6. python3爬虫 - 利用浏览器cookie登录

    http://blog.csdn.net/pipisorry/article/details/47980653 爬虫爬网站不免遇到需要登录的问题. 登录的时候可能还会碰到需要填验证码的问题, 有的验证 ...

  7. 使用Boostrap框架写一个登录\注册界面

    Bootstrap是一个Web前端开发框架,使用它提供的css.js文件可以简单.方便地美化HTML控件.一般情况下,对控件的美化需要我们自己编写css代码,并通过标签选择器.类选择器.ID选择器为指 ...

  8. DRF 商城项目 - 用户( 登录, 注册,登出,个人中心 ) 逻辑梳理

    用户登录 自定义用户登录字段处理 用户的登录时通过 手机号也可以进行登录 需要重写登录验证逻辑 from django.contrib.auth.backends import ModelBacken ...

  9. Node.js_express_中间件 middleware_登录/注册实例源代码

    静态资源: 都写死了的资源,如 css,html 解析规则: 所有路由和中间件都在一个数组中,js 引擎会按照代码先后顺序添加路由和中间件 当请求发送到服务器时,服务器获取当前的请求信息(请求方式.请 ...

随机推荐

  1. iphone屏幕镜像怎么用 手机投屏电脑

    手机看视频有的时候总会感觉到累,屏幕太小看的不够爽又或者用手一直拿着手机看累得慌.我就就喜欢看电视因为电视屏幕大看的爽,而且现在很多手机视频都可以往电视上投影视频,那么iphone屏幕镜像怎么用? 使 ...

  2. 使用hibernate报错java.lang.ExceptionInInitializerError的处理方法

    今天使用hibernate搭建持久层出现一个问题 原因在于  在创建user liberty时同时勾选了System Library(added to the boot class )path

  3. 我写的Angular相关的文章

    此文正在更新中... Angular6的变化 Angular7的变化 No value accessor for form control with path的解决方案

  4. python地理处理包——geopy使用之地理编码与反地理编码

    由于专业需要,经常接触一些地理处理的工具包,文档都是英文的,自己看的同时将其翻译一下,一方面自己学习的同时有个记录,要是能同时给一起的学习的童鞋们一些帮助,想想也是极好的.以下的文档内容主要翻译自官方 ...

  5. 网络控制器intel 82599, powerpc 830的BD描述符结构

    一.Intel 82599的BD结构 1.文档名称“82599-10-gbe-controller-datasheet.pdf”,可以从intel官网上下载. https://www.intel.cn ...

  6. 小程序实践(二):swiper组件实现轮播图效果

    swiper组件类似于Android中的ViewPager,实现类似轮播图的效果,相对于Android的Viewpager,swiper实现起来更加方便,快捷. 效果图: 首先看下swiper支持的属 ...

  7. mac 全角/半角标点符号切换

    快捷键:option+shift+H 背景是这样的,前段时间sublimeText新装了HTML/CSS/JS Prittify,JS代码格式化的快捷键是:command+shift+H. 记性有点差 ...

  8. 银盒子智慧餐厅硬件尺寸规格&推荐机型

  9. raid1 raid2 raid5 raid6 raid10的优缺点和做各自raid需要几块硬盘

    Raid 0:一块硬盘或者以上就可做raid0优势:数据读取写入最快,最大优势提高硬盘容量,比如3快80G的硬盘做raid0 可用总容量为240G.速度是一样.缺点:无冗余能力,一块硬盘损坏,数据全无 ...

  10. Python比较(关系)运算符

    比较(关系)运算符 运 算 符 作 用   举 例  结 果  >  大于 'a'>'b'   False  <  小于  156<456  True  ==  等于  'c' ...