码迷,mamicode.com
首页 > 编程语言 > 详细

Python实现注册和三次验证登录

时间:2018-10-29 21:38:23      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:lse   切割   对不起   用户   程序   完成   microsoft   登录   inpu   

# 帐户表account:
# sylar:123
# alex:456
# wusir:789
# taibai:789
# 需熟练的知识点:文件操作with open()/write()/read()、去掉所有空格strip()、切割split()、所有字母大写upper()
# 循环for...in...、判断if...else...
def regist():
# 输入数据
# 用户名若存在则不通过:注册失败
# 通过则存入account: mode="r+"
print("请输入用户名及密码完成注册")
username = input("请输入注册用户名:")
password = input("请输入密码")
with open("account", mode="r+", encoding="utf-8") as f:
for line in f: # sylar:123
if username == line.strip().split(":")[0]:
print("对不起,该用户已注册")
break
else:
f.write("\n"+username+":"+password)
print("注册名为:%s" % username)
print("注册的密码为:%s" % password)
regist()

def login():
count = 3
while count > 0:
count -= 1
username = input("请输入用户名:").strip()
password = input("请输入密码:").strip()
with open("account", mode="r", encoding="utf-8") as f:
for item in f:
if username +":"+password == item.strip():
return True
else:
print("密码错误,您还有%s 次机会" % count)
return "程序退出"
while 1:
print("注册: 1 登录: 2 退出: Q")
num = input("请输入:").strip()
if num.upper() == "Q":
break
elif num == "1":
regist()
elif num == "2":
if login():
print("登录成功!")
break
else:
print("登录失败!")
else:
print("输入错误! 重新输入!")

Python实现注册和三次验证登录

标签:lse   切割   对不起   用户   程序   完成   microsoft   登录   inpu   

原文地址:https://www.cnblogs.com/searchforyou/p/9873173.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!