def write_users_to_file():
f=open('account_info.txt','w',encoding='utf-8')
for user in user_list:
f.write(user['name'])
f.write('|')
f.write(user['pwd'])
f.write('|')
f.write(user['money'])
if 'locktime' in user:
f.write('|')
f.write(user['locktime'])
f.write(',')
f.close()
import time
user_list=[]
user_login={}
shopping_car=[]
def read_all_user():
if user_list:
return
with open('account_info.txt','rt',encoding='utf-8')as f:
text=f.read()
if not text:return
texts=text.split(',')
for i in texts:
temp=i.split('|')
print(temp)
d_dic={}
d_dic['name']=temp[0]
d_dic['pwd']=temp[1]
d_dic['money']=temp[2]
user_list.append(d_dic)
def register():
while True:
name=input('请输入用户名:').strip()
password=input('请输入密码:').strip()
if name and password:
print('ok')
if user_exist(name):
print('用户名已存在')
continue
else:
print('可以注册')
if len(password)<6:
continue
else:
print('注册成功')
write_user(name,password)
user_list.append({'name':name,'pwd':password,'money':'0'})
return
else:
print('用户或者密码不能为空')
def lock_user(username):
for user in user_list:
if user['name']==username:\
user['locktime']=str(time.time())
print(username+'被锁定')
print(user_list)
write_users_to_file()
def user_exist(username):
for user in user_list:
if user['name']==username:
return True
return False
def is_lock(username):
for user in user_list:
if user['name']==username:
if 'locktime' not in user:
return False
if time.time()-float(user['locktime'])<=10:
return True
error_dic={}
def login():
while True:
name=input('输入用户名 输入0返回上一层:')
if name =='0':
return
flag=False
for user in user_list:
if user['name']==name:
flag=True
break
else:
print('用户不存在,请先注册')
continue
password=input('请输入密码:')
for user in user_list:
if user['name']==name and user['pwd']==password:
print('登录成功,欢迎:%s'%name)
user_login['name']=name
user_login['pwd']=password
user_login['money']=user['money']
user_login['shopping_car']=shopping_car
return True
print('用户名或者密码不正确,请重新输入')
if name not in error_dic:
error_dic[name]=1
else:
error_dic[name]=1+error_dic[name]
num=error_dic[name]
print('剩余尝试次数%d'%(3-num))
if num==3:
lock_user(name)
def write_user(name,password):
with open('account_info.txt','a+',encoding='utf-8')as f:
old=',%s|%s|0'%(name,password)
f.write(old)
def shopping():
if not user_login:
print('请先注册或者登录')
return
while True:
shopping=[['1','口红',300],['2','棉袄',800],['3','棉裤',200],['4','方便面',10],['5','暖水壶',50]]
for i in shopping:
print('商品号:{},{}:{}'.format(i[0],i[1],i[2]))
num=input('请输入您要购买的商品序号或者商品名(输入0退出):')
if num =='0':
print(shopping_car)
return
for i in shopping:
if num not in i:
continue
else:
user_login['shopping_car'].append(i)
def show_shopping_car():
if not user_login:
print('请先注册或者登录')
return
else:
print(user_login['shopping_car'])
def account_info():
if not user_login:
print('请先注册或者登录')
return
print('账号:{}\n密码:{}'.format(user_login['name'],user_login['pwd']))
def add_money():
if not user_login:
print('请先注册或者登录')
return
while True:
try:
num=int(input('请输入充值的钱(输入0退出):'))
except:
print('请输入合法字符')
if num == '0':
return
for i in user_list:
i['name']=user_login['name']
i['money']=int(user_login['money'])+num
write_users_to_file()
method_dic={'1':register,
'2':login,
'3':shopping,
'4':show_shopping_car,
'5':account_info,
'6':add_money}
def main():
read_all_user()
while True:
print('''
欢迎使用x宝商城,请选择
1:注册
2:登录
3:购车
4:查看购物车
5:账号信息
6:账户充值
0:退出系统
''')
choice=input('请输入执行的功能:').strip()
if choice=='0':
return
if choice not in method_dic:
print('输入有误,请重新输入:')
continue
method_dic[choice]()
main()
回家作业兼笔记
最新推荐文章于 2024-06-08 17:36:50 发布