如何使用户名立即更新而不是重新运行程序

时间:2018-11-16 17:32:51

标签: python

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

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

filename1 =“ c:\ Users \ Anna Hamelin \ Documents \ Python Scripts \ SourceCode \ Project2 \ usernames.txt” file = 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)

2 个答案:

答案 0 :(得分:0)

您可以将该交互逻辑抽象为一个循环。您也可以使用with选项打开/关闭文件来删除一堆代码。这是一个用于更新用户和密码的原型。

您的身份验证是....不安全,哈哈,因为多个用户可以使用相同的密码等。我会根据用户和密码的组合创建一个哈希,但这就是一个不同的故事:)

def interact(user_names_ls, passwords_ls):
    goodlogin = False
    username = raw_input("Please enter your username: ")
    password = raw_input("Please enter your password: ")
    if username in user_names_ls and password in passwords_ls:
        goodlogin = True

    if goodlogin:
        print( "Access granted!")
    else:
        print("Incorrect Login credentials, please try again.")
        interact(user_names_ls, passwords_ls)


usernames_file_path = "usernames.txt"
with open(usernames_file_path, "r") as f:
    user_names_ls = f.readlines()
    user_names_ls = [user.rstrip() for user in user_names_ls]

passwords_file_path = "passwords.txt"
with open(passwords_file_path, "r") as f:
    passwords_ls = f.readlines()
    passwords_ls = [password.rstrip() for password in passwords_ls]


print("-"*50)
end_interaction = False

while not end_interaction:
    response = raw_input("-" * 50 + "\nWelcome! Do you have an account (y/n) [q to quit]? ")

    if response == "q":
        end_interaction = True

    #If user has an account:
    if response == "y":
        interact(user_names_ls, passwords_ls)

    #If user does not have account:
    else:
        newusername = raw_input("What is your new username? ")
        newpassowrd = raw_input("What is your new password? ")

        # here's where we can update our user and password cache
        user_names_ls.append(newusername)
        passwords_ls.append(newpassowrd)

# officially update the 'databases'
with open(usernames_file_path, "w") as f:
    user_names_ls = f.writelines(user_names_ls)


with open(usernames_file_path, "w") as f:
    passwords_ls = f.writelines(passwords_ls)

示例互动:

Welcome! Do you have an account (y/n) [q to quit]? n
What is your new username? he
What is your new password? 99
--------------------------------------------------
Welcome! Do you have an account (y/n) [q to quit]? y
Please enter your username: he
Please enter your password: 99
Access granted!
--------------------------------------------------
Welcome! Do you have an account (y/n) [q to quit]? 

btw-我在这里使用了raw_input,但是您可以将其与输入交换

答案 1 :(得分:0)

我添加了几个函数来减少代码重用。另外,我在login_user函数中加载了用户名和密码,以便它始终获取最新副本。

Query