python装饰器的问题
# 装饰器实现登入loggin_status=False
list=[]
def loggin(func):
if loggin_status == False:
def loggin_in():
user=input('user:')
passwd=input('password:')
with open('账号信息','r',) as file:
for i in file.readlines():
list.append(i.strip('\n'))
pass
if list.count(user) is not 0 and list.count(passwd) is not 0:
print('login.............')
global loggin_status
loggin_status=True
else:
print('err')
return loggin_in
@loggin
def shouji():
print('this pages is shouji')
@loggin
def dianqi(list):
print('this pages is dianqi')
while True:
print('''
欢迎使用
1.手机
2.电气
3.退出
''')
a=int(input('>>'))
if a in range(1,4):
if a==1:
shouji()
if loggin_status is False:
print('loggin_status为False')
else:
print('loggin_status为Ture')
elif a==2:
dianqi()
elif a==3:
break
else:
print('不再范围内....')
以上代码本意是想实现,选择后判断是否登入如果登入了就不需要再一次登入,如果没有登入则执行装饰器登入。但是没有实现,请大神指点一下!
以下为执行后的结果:
# 装饰器实现登入
loggin_status = False
list = []
# def loggins(func):
# if loggin_status == False:
# def loggin_in():
# user = input('user:')
# passwd = input('password:')
# with open('账号信息', 'r', ) as file:
# for i in file.readlines():
# list.append(i.strip('\n'))
# pass
# if list.count(user) is not 0 and list.count(passwd) is not 0:
# print('login.............')
# global loggin_status
# loggin_status = True
# else:
# print('err')
#
# return loggin_in
def login():
global loggin_status
user = input('user:')
passwd = input('password:')
if user == 'king' and passwd == '123456':
print("登陆成功!!!")
loggin_status = True
else:
print("密码错误!!!!")
loggin_status = False
def loggin(func):
def warpper(*args, **kwargs):
if not loggin_status:
login()
func()
return warpper
@loggin
def shouji():
print('this pages is shouji')
@loggin
def dianqi():
print('this pages is dianqi')
while True:
print('''
欢迎使用
1.手机
2.电气
3.退出
''')
a = int(input('>>'))
if a in range(1, 4):
if a == 1:
shouji()
if loggin_status is False:
print('loggin_status为False')
else:
print('loggin_status为Ture')
elif a == 2:
dianqi()
elif a == 3:
break
else:
print('不再范围内....') # 装饰器实现登入
loggin_status = False
user_dict = {}
# 数据库密码 文件格式
# user_db
# king=123456
# def login():
# global loggin_status
# user = input('user:')
# passwd = input('password:')
# if user == 'king' and passwd == '123456':
# print("登陆成功!!!")
# loggin_status = True
# else:
# print("密码错误!!!!")
# loggin_status = False
def login():
global loggin_status
user = input('user:')
password = input('password:')
with open('user_db', 'r', encoding='utf-8') as file:
for line in file.readlines():
u, p = line.strip().split('#')
if user == u and password == p:
print("登陆成功!!!")
loggin_status = True
break
else:
print("用户密码错误!!!")
loggin_status = False
def loggin(func):
def warpper(*args, **kwargs):
if not loggin_status:
login()
func()
return warpper
@loggin
def shouji():
print('this pages is shouji')
@loggin
def dianqi():
print('this pages is dianqi')
while True:
print('''
欢迎使用
1.手机
2.电气
3.退出
''')
a = int(input('>>'))
if a in range(1, 4):
if a == 1:
shouji()
if loggin_status is False:
print('loggin_status为False')
else:
print('loggin_status为Ture')
elif a == 2:
dianqi()
elif a == 3:
break
else:
print('不再范围内....')
页:
[1]