标签:打开 eval strftime else print images image one 密码验证

有四个文件
user_file:
{‘张三‘: ‘123abc‘, ‘hu‘: ‘123456‘, ‘Aelx‘: ‘tttt‘, ‘王五‘: ‘www123‘, ‘zhou‘: ‘12346‘, ‘lili‘: ‘eeettt‘}
lock_file :
[‘Noneegj‘, ‘lili‘, ‘hu‘, ‘zhou‘]
user_info :
{‘张三‘: {‘salary‘: 20000}, ‘hu‘: {‘salary‘: 380900}, ‘zhou‘: {‘salary‘: 386400}}
history :
{‘hu‘: "[[‘手机‘, 2300], [‘笔记本电脑‘, 3500], [‘平板电视‘, 3900]]", ‘zhou‘: "[[‘平板电视‘, 3900]]"}
# -*- coding:utf-8 -*-
# zhou
# 2017/6/24
import os, sys, time
goodsName = [
[‘手机‘, 2300],
[‘笔记本电脑‘, 3500],
[‘平板电视‘, 3900],
[‘电脑‘, 1300],
[‘电磁炉‘, 500]
]
userFile = open(‘user_file‘, ‘r+‘, encoding=‘utf-8‘) # 打开用户文件,转换成字典形式
userList = eval(userFile.readline())
lockFile = open(‘lock_file‘, ‘r+‘, encoding=‘utf-8‘) # 打开用户锁定文件,转换成列表形式
lockList = eval(lockFile.readline())
shopList = []
def register(userName):
‘‘‘
注册用户
:param userName: 注册用户名
:return:
‘‘‘
while True:
password1 = input("请输入您的密码:")
password2 = input("请再次输入您的密码:")
if password1 == password2:
# with open(‘user_file‘, ‘r+‘, encoding=‘utf-8‘) as f:
# tmp = eval(f.readline())
# tmp[userName] = password1
# f.seek(0)
# f.write(str(tmp))
userList[userName] = password1
userFile.seek(0)
userFile.write(str(userList))
print(‘注册完成.‘)
return True
else:
userInp = input("您两次密码不一致,注册失败,按y|Y直接退出,其余键重新输入密码")
if userInp == ‘y‘ or userInp == ‘Y‘:
confimLogout()
def userExit(userName):
‘‘‘
判断用户是否存在及是否锁定
:param userName:用户名
:return:True:代表用户存在,并且没有被锁定,False:用户不存在,或者用户被锁定
‘‘‘
if userName in userList and userName not in lockList:
return True
elif userName in userList and userName in lockList:
print("\033[31;1m{}\033[0m用户已经被锁定.请联系商家进行解锁.".format(userName))
else:
userInp = input("\033[31;1m{}\033[0m用户不存在.按y|Y进行注册.其余任意键返回".format(userName))
if userInp == ‘y‘ or userInp == ‘Y‘:
return register(userName)
def confimLogout():
‘‘‘
用户退出确认函数
:return:True:退出程序, False:不退出程序
‘‘‘
# userInp = input("是否退出!按[y|Y]确定退出?.其他任意键返回<<<.")
# if userInp == ‘y‘ or userInp == ‘Y‘:
print("再见,欢迎您下次光临.")
userFile.close()
lockFile.close()
exit()
def fileExit(fileName):
‘‘‘
判断文件是否存在, 不存在就创建一个名字为fileName,内容为{}的文件
:param fileName: 文件名
:return:
‘‘‘
if os.path.exists(fileName):
pass
else:
with open(fileName, ‘w‘, encoding=‘utf-8‘) as f:
f.write(‘{}‘)
def userPass():
‘‘‘
输入用户名和密码是否通过
:return: 如果用户名密码验证通过,返回userName
‘‘‘
while True:
print(‘用户登录‘.center(30, ‘-‘))
userName = input(‘请输入用户名,按y|Y直接退出商城:‘)
if userName == ‘y‘ or userName == ‘Y‘:
confimLogout()
elif userExit(userName):
# 判断用户是否存在并且没有被锁定
count = 3 # 定义输入密码的次数
while count > 0:
# 循环输入密码三次, 如果还不准确,账号将被锁定
password = input(‘请输入您的密码.(您还有\033[31;1m{}\033[0m次机会.)‘.format(count))
if userList[userName] == password:
return userName
count -= 1
lockList.append(userName)
lockFile.seek(0)
lockFile.write(str(lockList))
userInp = input(‘您的用户被锁定.请联系商家进行解锁.按y|Y直接退出商城.其他任意键重新登录.‘)
if userInp == ‘y‘ or userInp == ‘Y‘:
confimLogout()
def userInfo():
with open(‘user_info‘, ‘r‘, encoding=‘utf-8‘) as f:
return eval(f.readline())
def queryRecord(userName):
‘‘‘
查询购物记录的函数,从history文件中进行查询
:param userName: 查询的用户名
:return:
‘‘‘
with open(‘history‘, ‘r+‘, encoding=‘utf-8‘) as f:
tmp = eval(f.readline())
if userName in tmp:
print("您之前购买过:".center(30, "-"))
print(tmp[userName])
else:
print("此用户暂时还没有购物记录.")
def shopping(userName, salary):
while True:
# 打印商品
print("能够购买的商品".center(30, ‘-‘))
for index, item in enumerate(goodsName, 1):
print(index, item)
print()
index = input("请输入您要购买的商品:")
if index.isdigit():
index = int(index)
if index > 0 and index <= len(goodsName):
if salary >= goodsName[index - 1][1]:
shopList.append(goodsName[index - 1])
salary -= goodsName[index - 1][1]
userInp = input("已经将\033[31;1m{}\033[0m加入您的购物车,按y|Y进行结算,其余任意键继续展示商品.".format(goodsName[index - 1]))
if userInp == ‘y‘ or userInp == ‘Y‘:
# 填入购物信息
with open(‘history‘, ‘r+‘, encoding=‘utf-8‘) as f:
tmp = eval(f.readline())
if userName not in tmp:
tmp[userName] = ‘‘
tmp[userName] = tmp[userName] + str(shopList)
f.seek(0)
f.write(str(tmp))
# 填入余额
with open(‘user_info‘, ‘r+‘, encoding=‘utf-8‘) as f:
tmp = eval(f.readline())
if userName not in tmp:
tmp[userName] = {}
tmp[userName][‘salary‘] = salary
f.seek(0)
f.write(str(tmp))
print("您已经购买了以下商品\033[31;1m{}\033[0m".format(shopList))
print("您还剩余额\033[31;1m{}\033[0m".format(salary))
confimLogout()
else:
choice = ("您的余额不足, 按y|Y进行充值,按其余任意键选择其他商品")
if choice == ‘y‘ or choice == ‘Y‘:
deposit(userName)
else:
print("请输入正确的商品编号.")
else:
print("请输入正确的商品编号.")
def deposit(userName):
‘‘‘
充值函数,往user_info文件中进行充值
:param userName: 用户名, 表示给哪个用户进行充值
:return:
‘‘‘
while True:
salary = input("请输入您要充值的金额:")
if salary.isdigit():
salary = int(salary)
if salary > 0:
with open(‘user_info‘, ‘r+‘, encoding=‘utf-8‘) as f:
tmp = eval(f.readline())
tmp[userName] = {} # 为用户添加一个新的字典
tmp[userName][‘salary‘] = salary # 为用户充值
f.seek(0)
f.write(str(tmp)) # 把用户的信息写入文件中
break
else:
print("输入的金额大于零,请重新输入.")
else:
print("输入的金额必须是数字.请重新输入")
# 主程序
fileExit(‘user_info‘)
fileExit(‘history‘)
print("欢迎来到\033[31;1m购物商城\033[0m")
time_format = ‘%y-%m-%d %X‘ #定义时间格式
times = time.strftime(time_format)
userNamePass = userPass()
print(userNamePass, userInfo())
if userNamePass not in userInfo():
# 判断用户是否在user_info中,如果不在就是第一次登录,就进行充值
deposit(userNamePass)
infoList = userInfo() # 获取用户信息的列表
userSalary = infoList[userNamePass][‘salary‘] # 获取用户此前的金额
# 打印用户信息
print(‘\n您的用户信息:‘)
print(‘用户名:\033[31;1m{}\033[0m‘.format(userNamePass))
print(‘余额:\033[31;1m{}\033[0m‘.format(userSalary))
while True:
# 开始购物
print(‘‘‘
按1 >>> 查询购物记录
按2 >>> 显示商品列表开始购物
按3 >>> 直接退出商城
‘‘‘)
userInp = input("请输入相应的编号.")
if userInp.isdigit():
userInp = int(userInp)
if userInp == 1:
queryRecord(userNamePass)
elif userInp == 2:
shopping(userNamePass, userSalary) # 哪个用户购买的商品
elif userInp == 3:
exit(‘您已经退出购物商城.‘)
else:
print(‘您输入的有误,请重新进行选择.‘)
else:
print("请输入正确的数字.")
标签:打开 eval strftime else print images image one 密码验证
原文地址:http://www.cnblogs.com/huwentao/p/7073825.html