通过open函数创建文件及读写文件,将用户名作为文件名,密码作为文件内容保存在目录下,登录时读取用户名命名的文件的内容,检测是否与输入的密码匹配,若匹配输出欢迎信息;如果不匹配,重新输入用户名密码,三次以后,将该文件内信息清除。登录时检测用户名的文件内容是否为空,若为空则判定为被锁定的文件,直接输出该文件已被锁定。
'''
This is a user login system.
You can sign up or sign in this system.
If you have a account in this system and want to sign in.
You have 3 times to login the system.
Or, your account will be locked by the system.
'''
#登录界面
print("**************************************************")
print("* WELCOME *")
print("**************************************************")
print(" Enter a number to select a task ")
print(" 1.sign in ")
print(" 2.sign up ")
# 是否锁定
locked = 0
#用户选择
user_choice = input()
#选择错误
if (user_choice != "1") and (user_choice != "2"):
print(" Wrong Command! ")
#登录系统
if user_choice == "1":
# 重复三次
for i in range(3):
# 输入账户名称
print("Please enter your account name:",end="")
user_account_name = input()
# 查找账户是否被锁定
user_account = open(user_account_name +'.log','r')
if user_account.read() == '':
print("Your account have been locked!")
locked = 1
break
else:
user_account.close()
# 输入密码
print("Enter your account code:",end='')
user_account_code_input = input()
user_account = open(user_account_name + '.log', 'r')
user_account_code = user_account.read()
user_account.close()
# 密码输入正确,跳出循环
if user_account_code == user_account_code_input:
break
# 密码输入错误,继续循环,并提示剩余次数
else:
print("Wrong code, you have " + str(2 - i) + " times")
# 输入三次密码不对,保留用户,清空数据,表示锁定
if i == 2 :
print("Sorry, your account have been locked.")
user_account = open(user_account_name + '.log', 'w')
user_account.write("")
user_account.close()
# 已被锁定的账户
elif locked == 1:
print("",end='')
# 用户名,密码输入正确,进入系统,提示Welcome
else:
print("Welcome, "+user_account_name + "!")
user_account.close()
user_account.closed
#注册账号
if user_choice == "2":
# 提示输入名户名
print("Please enter your account name:",end='')
user_account_name = input()
user_account = open(user_account_name+'.log','w')
user_account.close()
# 输入密码,重复验证机制,如果密码不一样,重新输入
code_examination = 0
while code_examination !=1:
print("Please enter a code:",end='')
user_account_code = input()
print("Please enter the code again:",end='')
user_account_code_again = input()
if user_account_code == user_account_code_again:
code_examination = 1
else:
print("The codes are not the same!")
# 写入密码
user_account = open(user_account_name + '.log', 'w')
user_account.write(user_account_code)
user_account.close()
**************************************************
* WELCOME *
**************************************************
Enter a number to select a task
1.sign in
2.sign up
2
Please enter your account name:qwer
Please enter a code:123
Please enter the code again:1234
The codes are not the same!
Please enter a code:123
Please enter the code again:123
此时会创建一个qwe.log的文件,其文件名为用户名,内容为密码
**************************************************
* WELCOME *
**************************************************
Enter a number to select a task
1.sign in
2.sign up
1
Please enter your account name:qwer
Enter your account code:321
Wrong code, you have 2 times
Please enter your account name:qwer
Enter your account code:123
Welcome, qwer!
输入错误密码会提示剩余次数,输入正确密码后可以进入系统并提示欢迎信息
**************************************************
* WELCOME *
**************************************************
Enter a number to select a task
1.sign in
2.sign up
1
Please enter your account name:qwer
Enter your account code:321
Wrong code, you have 2 times
Please enter your account name:qwer
Enter your account code:321
Wrong code, you have 1 times
Please enter your account name:qwer
Enter your account code:321
Wrong code, you have 0 times
Sorry, your account have been locked.
如果密码输入错误三次则会提示账户已锁定,再次登录效果如下:
**************************************************
* WELCOME *
**************************************************
Enter a number to select a task
1.sign in
2.sign up
1
Please enter your account name:qwer
Your account have been locked!
此时直接显示账户被锁定
而相对应的,qwer.log内的内容被清空,以说明此账号已被锁定
后续会加入其它函数判断是否存在该文件,以判断用户是否注册,并给出相应提示。
程宇。
修改于2017.11.25