簡體   English   中英

檢查用戶名是否已經存在

[英]Checking to see if username already exists

從“ #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()

代碼的問題在於您正在打印消息“確定,請繼續!”。 在您的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”變量檢查您的usernameslist [id]時,都會運行打印語句“ print(“ Ok,請繼續!”)),從而導致該消息多次顯示。

解決此問題的方法是將打印語句移出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.")
    ...

看起來您正在遍歷整個用戶名列表,並在每次迭代中打印出成功或錯誤消息。 嘗試以下方法:

# 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: 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM