python学习笔记day08 用户注册,登录功能实现

先说下需求:

用户注册,将用户名和密码写入文件中(相当于用户注册);

然后当用户输入用户名和密码时,看能不能成功匹配,有三次尝试机会;

当机会用完时,可以根据需求重新赋予机会;

username=input("用户名>>>")
password=input('密码>>>')
info=[]  #存放用户信息
with open('info',mode='w+',encoding='utf-8') as file:
    file.write(username+',')  #用户名写完之后用逗号隔开密码,到时候读取 split()方便获取用户名和密码
    file.seek(file.tell())  #写入用户名之后,file.tell()找到光标位置
    file.write(password) #开始写密码
flag,count=1,3
while flag:
    username_1=input("用户名>>>")
    password_1=input("密码>>>")
    with open("info",mode='r+',encoding='utf-8') as file2:
        username,password=file2.readline().split(',')
    if username_1==username and password_1==password:
        print("恭喜登录成功~")
        flag=0
    else:
        if input('try again (Y or N):').upper=='N':
            flag=0
        else:
            count-=1
            print("您还有{}次机会尝试".format(count))
            if count==0:
                print("您的机会已经用完~")
                msg=input('是否还需要机会尝试?(Y or N)')
                if msg.upper()=='Y':
                    count=3
                else:
                    flag=0

完成啦~

talk is cheap,show me the code
原文地址:https://www.cnblogs.com/xuanxuanlove/p/9531869.html