首页 > 解决方案 > 如何立即更新用户名而不是重新运行程序

问题描述

该程序用于检查用户名和密码以允许用户进入。但是,在我添加新用户的最后,我希望用户能够立即尝试导入他们新创建的用户名和密码,以便他们可以输入程序,在将用户名和密码添加到数据库后无需重新运行。但是,此代码返回错误“列表索引超出范围”

第 1 阶段:打开文件并获取数据

filename1 = "c:\Users\Anna Hamelin\Documents\Python Scripts\SourceCode\Project2\usernames.txt" 文件 = open(filename1, "r")

#Usernames
users = file.read()
usernameslist = [line.strip() for line in open("c:\\Users\\Anna Hamelin\\Documents\\Python Scripts\\SourceCode\\Project2\\usernames.txt")]
#print(users)                #Check file
#print(usernameslist)       #Check usernames list
file.close()

filename2 = "c:\\Users\\Anna Hamelin\\Documents\\Python Scripts\\SourceCode\\Project2\\passwords.txt"
file = open(filename2, "r")

#Passwords
passwords = file.read()
passwordslist = [line.strip() for line in open("c:\\Users\\Anna Hamelin\\Documents\\Python Scripts\\SourceCode\\Project2\\passwords.txt")]
#print(passwords)            #Check file
#print(passwordslist)       #Check passwords list
file.close()

#Compile the usernames and passwords lists for easy checking
#compiled_list = list(zip(usernameslist,passwordslist))
#print(compiled_list)

#Scores
filename3 = "c:\\Users\\Anna Hamelin\\Documents\\Python Scripts\\SourceCode\\Project2\\scores.txt"
file = open(filename3, "r")

scores = file.read()
scoreslist = [line.strip() for line in open("c:\\Users\\Anna Hamelin\\Documents\\Python Scripts\\SourceCode\\Project2\\scores.txt")]
#print(scores)           #Check file
#print(scoreslist)       #Check scores
file.close()

#STAGE 2
#Print Welcome/Intro message

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

#If user has an account:
if response == "y":
    #Create a login function:
    #def login():
        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)
        else:
            print(print_in_red + "Incorrect Login credentials, please try again." + print_default)
    #login()

#If user does not have account:
else: 
    newusername = input("What is your new username? ")
    newpassowrd = input("What is your new password? ")
    file = open(filename1, "a")
    file.write("\n" + newusername)
    file = open(filename2, "a")
    file.write("\n" + newpassowrd)
    file.close
    print("Now that you have created an account, please continue.")
    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)
    else:
            print(print_in_red + "Incorrect Login credentials, please try again." + print_default)

标签: python

解决方案


我添加了几个函数来减少代码重用。我还在 login_user 函数中加载了用户名和密码,所以它总是得到最新的副本。

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("Access granted!")
            else:
                print("Incorrect Login credentials, please try again.")

    #If user does not have account:
    else:
        newusername = input("What is your new username? ")
        newpassword = input("What is your new password? ")
        add_file_contents(users_path, '\n' + newusername)
        add_file_contents(passwords_path, '\n' + newpassword)
        login_user(new_account=True)

login_user()

推荐阅读