检查用户名是否已经存在

时间:2018-11-19 13:26:40

标签: python

从“ #If用户没有帐户:”开始,代码多次打印输出。我只希望它打印输出一次(要么使用用户名,要么可以继续使用)。然后,我希望输入两次密码并进行比较以确保密码匹配。您能帮我解决这个问题吗?

import colorama
colorama.init()

print_in_green = "\x1b[32m"
print_in_red = "\x1b[31m"
print_in_blue = "\x1b[36m"
print_in_pink = "\x1b[35m"
print_default = "\x1b[0m"

#STAGE 1: Opening the files and grabbing data
users_path = "c:\\Users\\Anna Hamelin\\Documents\\Python Scripts\\SourceCode\\Project2\\usernames.txt"
passwords_path = "c:\\Users\\Anna Hamelin\\Documents\\Python Scripts\\SourceCode\\Project2\\passwords.txt"
scoreslist_path = "c:\\Users\\Anna Hamelin\\Documents\\Python Scripts\\SourceCode\\Project2\\scores.txt"


def get_file_contents(file_path):
    return [line.strip() for line in open(file_path)]

scoreslist = get_file_contents(scoreslist_path)

def add_file_contents(file_path, contents):
    with open(file_path, "a") as file:
        file.write(contents)

def login_user(new_account=False):
    usernameslist = get_file_contents(users_path)
    passwordslist = get_file_contents(passwords_path)

    if new_account:
        response = 'y'
    else:
        response = input("-"*50 + "\nWelcome! Do you have an account (y/n)? ")
        print("-"*50)

    #If user has an account:
    if response == "y":
            goodlogin = False
            username = input("Please enter your username: ")
            password = input("Please enter your password: ")
            for id in range(len(usernameslist)):
                if username == usernameslist[id] and password == passwordslist[id]:
                    goodlogin = True

            if goodlogin:
                print(print_in_green + "Access granted!" + print_default)
                #Ask if user would like to view leaderboard
                leaderboard = input("Would you like to view the leaderboard (y/n)? ")

                #If thet want to see scores:
                if leaderboard == "y":
                    print("-"*50 + "\n" + print_in_blue + "Here is the leaderboard!\n" + print_default + "-"*50)
                    for c in range(0, len(scoreslist)-1):
                        max = scoreslist[c]
                        index_of_max = c
                        for i in range (c+1, len(scoreslist)):
                            if (scoreslist[i] > max):
                                max = scoreslist[i]
                                index_of_max = i
                        aux = scoreslist[c]
                        scoreslist[c] = max
                        scoreslist[index_of_max] = aux
                        #print(scoreslist)
                    print(*scoreslist, sep = "\n")
                    print("-"*50)
                    #If they don't want to see scores:
                else:
                    print("OK. Thanks for loging in!")


            else:
                print(print_in_red + "Incorrect Login credentials, please try again by restarting." + print_default)

    #If user does not have account:
    else:
        goodlogin2 = False
        newusername = input("What is your new username? ")
        for id in range(len(usernameslist)):
            if newusername != usernameslist[id]:
                goodlogin2 = True
                print("Ok, please continue!")
            else:
                print("This username is already taken. Please try another.")

        newpassword = input("What is your new password? ")
        newpasswordagain = input("Please enter your new password again.")
        if newpassword == newpasswordagain:
            print("Please follow the instructions to log in with your new credentials.")
            add_file_contents(users_path, '\n' + newusername)
            add_file_contents(passwords_path, '\n' + newpassword)
            login_user(new_account=True)
        else:
            print("Your passwords do not match. Please try again.")

login_user()

2 个答案:

答案 0 :(得分:0)

代码的问题在于您正在打印消息“确定,请继续!”。在您的for循环中。

...
for id in range(len(usernameslist)):
        if newusername != usernameslist[id]:
            goodlogin2 = True
            print("Ok, please continue!")
        else:
            print("This username is already taken. Please try another.")
...

发生的事情是,每次对照'newusername'变量检查您的用户名列表[id]时,都会运行打印语句'print(“ Ok,please继续!”)),从而导致该消息多次显示。

解决此问题的方法是将打印语句移出for循环,因此,假设新的用户名与用户名列表中的任何用户名都不匹配,则显示消息“确定,请继续!”会提示用户输入两次密码,然后向用户显示一次。

以下是适合您的内容:

...
for id in range(len(usernameslist)):
        if newusername != usernameslist[id]:
            goodlogin2 = True
        else:
            print("This username is already taken. Please try another.")

if goodlogin2 = True:
    print("Ok, please continue!")

    newpassword = input("What is your new password? ")
    newpasswordagain = input("Please enter your new password again.")
    ...

答案 1 :(得分:0)

似乎您要遍历整个用户名列表,并在每次迭代中打印出成功或错误消息。 尝试以下方法:

# If user does not have account:
else:
    goodlogin2 = False
    newusername = input("What is your new username? ")
    if newusername in usernameslist:
        print("This username is already taken. Please try another.")
    else:
        goodlogin2 = True
        print("Ok, please continue!")
    # for id in range(len(usernameslist)):
    #     if newusername != usernameslist[id]:
    #         goodlogin2 = True
    #         print("Ok, please continue!")
    #     else:
    #         print("This username is already taken. Please try another.")

    newpassword = input("What is your new password? ")
    newpasswordagain = input("Please enter your new password again.")
    if newpassword == newpasswordagain:
        print("Please follow the instructions to log in with your new credentials.")
        add_file_contents(users_path, '\n' + newusername)
        add_file_contents(passwords_path, '\n' + newpassword)
        login_user(new_account=True)
    else:
        print("Your passwords do not match. Please try again.")

对于没有先前帐户输入未使用名称的用户,这将产生以下输出:

--------------------------------------------------
Welcome! Do you have an account (y/n)? n
--------------------------------------------------
What is your new username? not_taken2
Ok, please continue!
What is your new password? pwd2
Please enter your new password again.pwd2
Please follow the instructions to log in with your new credentials.
Please enter your username: 
相关问题