当前位置:网站首页>Register login 1

Register login 1

2022-04-22 02:00:00 Right ear to refuel

 
 
2.2: Write a program to realize user registration ( Register in file ), You can log in. ( The login information comes from the file )
#  Tips :

while True:
    msg = """
    0  sign out 
    1  Sign in 
    2  register 
    """
    print(msg)
    cmd = input(' Please enter the command number >>: ').strip()
    if not cmd.isdigit():
        print(' You must enter a number for the command number , silly ass ')
        continue

    if cmd == '0':
        break
    elif cmd == '1':
        #  Login function code ( additional : You can nest previous loops , Three wrong input exits are introduced )
        # print(f" The account password entered by the user is :{name},{passward}")
        # 2, Read the user's data from the file for verification 
        tag=True
        namelist = {}
        while tag:
            with open(" Account password book ", "r", encoding="utf-8") as f:
                for line in f:
                    a,b,c=line.strip().split(":")

                # res = f.read().strip()
                # a,b = res.split(":")
                    namelist[a]=line.strip().split(":")

                    # print(f" The account password stored in the file is :{a},{b},{c}")
                # namelist=dict(set(namelist))
                print(namelist)


                name = input(" Please enter your account number :").strip()
                print(namelist[name])
                if name in namelist:
                    count = 0
                    while count < 3:
                        passward = input(" Please enter your password :").strip()
                        if passward in namelist[name]:
                            print(" Landing successful ")
                            tag = False
                            break
                        else:
                            print(f" Login failed , Please input the password again , Do you have {2-count}")
                            count+=1
                # else:
                #     print(" Incorrect account number , Please re-enter "
                #     )
        break
    elif cmd == '2':
        #  Register function code 
        username = input(" Please enter a user name :").strip()
        passward=input(" Please input a password :").strip()
        with open(" Account password book ","a+",encoding="utf-8")as f:
            f.write("%s:%s:%d"%(username,passward,1,))
            f.write("\n")
    else:
        print(' The command you entered does not exist ')

    #  reflection : The above if Whether the function of branching can be realized in other more elegant ways 

Code thinking : Two big structures : Sign in and sign up . Registration is to send information through w The mode is written into the account password file , Login is through r Mode read information . This is just a big direction . When using the cycle to read the information of the account password book, the pointer is at the end , So you need to put the read data into a dictionary . Finally, just check whether the account number is in the dictionary . utilize while Cycle to control the number of reads , This code does not realize the function of account locking

版权声明
本文为[Right ear to refuel]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211528165758.html