This program works to check usernames and passwords to let a user in. However, at the end where I am adding a new user, I want the user to immediately be able to try to import their newly created username and password so they can enter the program, without re-running after their username and password are added to the database. However, this code is returning the error "list index is out of range"
STAGE 1: Opening the files and grabbing data
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)