用文本文件验证用户名和密码。使用任何密码的用户名

时间:2019-11-20 09:25:33

标签: python passwords username verification

因此,该程序可以正常运行,并与名为用户名和密码的文本文件一起运行。但是,当文本文件中包含用户名和密码时,我不太知道如何停止使用任何密码的用户名,因为我只希望用户名与密码在同一行上使用。

import random
Empty = []
userfile = open("username.txt","r")
print("Welcome to my custom made game, please login so we know your a verified user.")
def readData(): #Load the files and log in
        Empty = []
        for each_line in myfile:
                Empty.append(each_line)
        print(Empty)
#myfile = []
#for line in userfile:
   #     myfile.append(line)

#readData()
def password():
        myfile = open("username.txt","r")
        passw = input("Please enter the password:")
        for line in myfile:
                eachline=line.split(",")
                if eachline[0].strip() and eachline[1].strip() == passw:
                        print("Valid password")
                        break
        else:
                print("Incorrect please try again")
                password()

def username():
        myfile = open("username.txt","r")
        user = input("Please enter the username:")
        for line in myfile:
                eachline=line.split(",")
                if eachline[0] == user:
                        print("Valid username")
                        password()
                        break
        else:
                print("Incorrect please try again")
                username()
username()

#password()




1 个答案:

答案 0 :(得分:0)

尝试

import random
Empty = []
userfile = open("username.txt","r")
print("Welcome to my custom made game, please login so we know your a verified user.")
def readData(): #Load the files and log in
        Empty = []
        for each_line in myfile:
                Empty.append(each_line)
        print(Empty)
#myfile = []
#for line in userfile:
   #     myfile.append(line)

#readData()
def password(username):
    myfile = open("username.txt","r")
    username = username
    tryAgain=True
    while tryAgain:
        passw = input("Please enter the password: ")
        for line in myfile:
            name=line.split(",")[0]
            if username == name:
                password=line.split(",")[1]
                pa=password.split("\n")[0]
            if pa == passw:
                print("Valid password")
                tryAgain = False
                continue
            else:
                validate = input("Incorrect password. Do you want to try again (y/n)): ")
                if "y" in validate:
                    break
                else:
                    print("goodbye")
                    tryAgain = False
                    return 0


def username():
    myfile = open("username.txt","r")
    user = input("Please enter the username: ")
    for line in myfile:
        eachline=line.split(",")
        if eachline[0] == user:
            print("Valid username")
            password(user)
            break
        else:
            print("Incorrect please try again")
            username()
username()