How to make username update immediately rather than re-running program





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















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:UsersAnna HamelinDocumentsPython ScriptsSourceCodeProject2usernames.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)









share|improve this question




















  • 1





    Not related, but note, file.close should be file.close()

    – juanpa.arrivillaga
    Nov 16 '18 at 17:40






  • 1





    Anyway, it sounds like you want to add some sort of loop that keeps querying the user to either continue with the program or exit, is that what you are looking for? This may be a bit broad.

    – juanpa.arrivillaga
    Nov 16 '18 at 17:41











  • @juanpa.arrivillaga I want to use the code from above to allow the user to login directly after creating a new username or password

    – Anna Hamelin
    Nov 16 '18 at 17:48











  • Then probably just wrap your main if ... else in a while True:... loop. You'll have to decide how you want to handle when the loop terminates. Check out the answers to this related question for inspiration.

    – juanpa.arrivillaga
    Nov 16 '18 at 17:53











  • In particular, see this extensive answer

    – juanpa.arrivillaga
    Nov 16 '18 at 18:09


















1















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:UsersAnna HamelinDocumentsPython ScriptsSourceCodeProject2usernames.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)









share|improve this question




















  • 1





    Not related, but note, file.close should be file.close()

    – juanpa.arrivillaga
    Nov 16 '18 at 17:40






  • 1





    Anyway, it sounds like you want to add some sort of loop that keeps querying the user to either continue with the program or exit, is that what you are looking for? This may be a bit broad.

    – juanpa.arrivillaga
    Nov 16 '18 at 17:41











  • @juanpa.arrivillaga I want to use the code from above to allow the user to login directly after creating a new username or password

    – Anna Hamelin
    Nov 16 '18 at 17:48











  • Then probably just wrap your main if ... else in a while True:... loop. You'll have to decide how you want to handle when the loop terminates. Check out the answers to this related question for inspiration.

    – juanpa.arrivillaga
    Nov 16 '18 at 17:53











  • In particular, see this extensive answer

    – juanpa.arrivillaga
    Nov 16 '18 at 18:09














1












1








1








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:UsersAnna HamelinDocumentsPython ScriptsSourceCodeProject2usernames.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)









share|improve this question
















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:UsersAnna HamelinDocumentsPython ScriptsSourceCodeProject2usernames.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)






python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 18:05







Anna Hamelin

















asked Nov 16 '18 at 17:32









Anna HamelinAnna Hamelin

197




197








  • 1





    Not related, but note, file.close should be file.close()

    – juanpa.arrivillaga
    Nov 16 '18 at 17:40






  • 1





    Anyway, it sounds like you want to add some sort of loop that keeps querying the user to either continue with the program or exit, is that what you are looking for? This may be a bit broad.

    – juanpa.arrivillaga
    Nov 16 '18 at 17:41











  • @juanpa.arrivillaga I want to use the code from above to allow the user to login directly after creating a new username or password

    – Anna Hamelin
    Nov 16 '18 at 17:48











  • Then probably just wrap your main if ... else in a while True:... loop. You'll have to decide how you want to handle when the loop terminates. Check out the answers to this related question for inspiration.

    – juanpa.arrivillaga
    Nov 16 '18 at 17:53











  • In particular, see this extensive answer

    – juanpa.arrivillaga
    Nov 16 '18 at 18:09














  • 1





    Not related, but note, file.close should be file.close()

    – juanpa.arrivillaga
    Nov 16 '18 at 17:40






  • 1





    Anyway, it sounds like you want to add some sort of loop that keeps querying the user to either continue with the program or exit, is that what you are looking for? This may be a bit broad.

    – juanpa.arrivillaga
    Nov 16 '18 at 17:41











  • @juanpa.arrivillaga I want to use the code from above to allow the user to login directly after creating a new username or password

    – Anna Hamelin
    Nov 16 '18 at 17:48











  • Then probably just wrap your main if ... else in a while True:... loop. You'll have to decide how you want to handle when the loop terminates. Check out the answers to this related question for inspiration.

    – juanpa.arrivillaga
    Nov 16 '18 at 17:53











  • In particular, see this extensive answer

    – juanpa.arrivillaga
    Nov 16 '18 at 18:09








1




1





Not related, but note, file.close should be file.close()

– juanpa.arrivillaga
Nov 16 '18 at 17:40





Not related, but note, file.close should be file.close()

– juanpa.arrivillaga
Nov 16 '18 at 17:40




1




1





Anyway, it sounds like you want to add some sort of loop that keeps querying the user to either continue with the program or exit, is that what you are looking for? This may be a bit broad.

– juanpa.arrivillaga
Nov 16 '18 at 17:41





Anyway, it sounds like you want to add some sort of loop that keeps querying the user to either continue with the program or exit, is that what you are looking for? This may be a bit broad.

– juanpa.arrivillaga
Nov 16 '18 at 17:41













@juanpa.arrivillaga I want to use the code from above to allow the user to login directly after creating a new username or password

– Anna Hamelin
Nov 16 '18 at 17:48





@juanpa.arrivillaga I want to use the code from above to allow the user to login directly after creating a new username or password

– Anna Hamelin
Nov 16 '18 at 17:48













Then probably just wrap your main if ... else in a while True:... loop. You'll have to decide how you want to handle when the loop terminates. Check out the answers to this related question for inspiration.

– juanpa.arrivillaga
Nov 16 '18 at 17:53





Then probably just wrap your main if ... else in a while True:... loop. You'll have to decide how you want to handle when the loop terminates. Check out the answers to this related question for inspiration.

– juanpa.arrivillaga
Nov 16 '18 at 17:53













In particular, see this extensive answer

– juanpa.arrivillaga
Nov 16 '18 at 18:09





In particular, see this extensive answer

– juanpa.arrivillaga
Nov 16 '18 at 18:09












2 Answers
2






active

oldest

votes


















0














You can abstract that interaction logic out into a loop. You can also remove a bunch of code with you use the with option of opening/closing files. Here's a prototype to keep users and passwords updated.



Your authentication is .... not safe haha, since multiple users can have the same password etc. I would create a hash out of the combination of the user and password, but thats a different story :)



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)


Sample interaction:



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 - I used raw_input here but you can just swap that with input






share|improve this answer































    0














    I added a couple of functions to reduce code reuse. Also I load the usernames and passwords in the login_user function so it always gets the latest copy.



    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()





    share|improve this answer


























    • This is so helpful! I got a NameError: name 'get_file_contents' is not defined. How can I fix it?

      – Anna Hamelin
      Nov 16 '18 at 18:21











    • Did you copy/paste the code as is? It should work. It was running on my machine. get_file_contents is the function defined on line 7.

      – Jason
      Nov 16 '18 at 19:23











    • I copied and pasted. I can't figure out why I am getting that error.

      – Anna Hamelin
      Nov 16 '18 at 19:51











    • Guess I had it commented it when I ran it before. Moved scoreslist below the function declaration and fixed it. Should work for you now if you copy / paste it. Sorry about that.

      – Jason
      Nov 16 '18 at 20:08











    • This worked perfectly, thank you so so much for your help!!!

      – Anna Hamelin
      Nov 16 '18 at 20:36












    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53342755%2fhow-to-make-username-update-immediately-rather-than-re-running-program%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can abstract that interaction logic out into a loop. You can also remove a bunch of code with you use the with option of opening/closing files. Here's a prototype to keep users and passwords updated.



    Your authentication is .... not safe haha, since multiple users can have the same password etc. I would create a hash out of the combination of the user and password, but thats a different story :)



    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)


    Sample interaction:



    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 - I used raw_input here but you can just swap that with input






    share|improve this answer




























      0














      You can abstract that interaction logic out into a loop. You can also remove a bunch of code with you use the with option of opening/closing files. Here's a prototype to keep users and passwords updated.



      Your authentication is .... not safe haha, since multiple users can have the same password etc. I would create a hash out of the combination of the user and password, but thats a different story :)



      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)


      Sample interaction:



      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 - I used raw_input here but you can just swap that with input






      share|improve this answer


























        0












        0








        0







        You can abstract that interaction logic out into a loop. You can also remove a bunch of code with you use the with option of opening/closing files. Here's a prototype to keep users and passwords updated.



        Your authentication is .... not safe haha, since multiple users can have the same password etc. I would create a hash out of the combination of the user and password, but thats a different story :)



        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)


        Sample interaction:



        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 - I used raw_input here but you can just swap that with input






        share|improve this answer













        You can abstract that interaction logic out into a loop. You can also remove a bunch of code with you use the with option of opening/closing files. Here's a prototype to keep users and passwords updated.



        Your authentication is .... not safe haha, since multiple users can have the same password etc. I would create a hash out of the combination of the user and password, but thats a different story :)



        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)


        Sample interaction:



        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 - I used raw_input here but you can just swap that with input







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 18:02









        LeKhan9LeKhan9

        1,1001113




        1,1001113

























            0














            I added a couple of functions to reduce code reuse. Also I load the usernames and passwords in the login_user function so it always gets the latest copy.



            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()





            share|improve this answer


























            • This is so helpful! I got a NameError: name 'get_file_contents' is not defined. How can I fix it?

              – Anna Hamelin
              Nov 16 '18 at 18:21











            • Did you copy/paste the code as is? It should work. It was running on my machine. get_file_contents is the function defined on line 7.

              – Jason
              Nov 16 '18 at 19:23











            • I copied and pasted. I can't figure out why I am getting that error.

              – Anna Hamelin
              Nov 16 '18 at 19:51











            • Guess I had it commented it when I ran it before. Moved scoreslist below the function declaration and fixed it. Should work for you now if you copy / paste it. Sorry about that.

              – Jason
              Nov 16 '18 at 20:08











            • This worked perfectly, thank you so so much for your help!!!

              – Anna Hamelin
              Nov 16 '18 at 20:36
















            0














            I added a couple of functions to reduce code reuse. Also I load the usernames and passwords in the login_user function so it always gets the latest copy.



            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()





            share|improve this answer


























            • This is so helpful! I got a NameError: name 'get_file_contents' is not defined. How can I fix it?

              – Anna Hamelin
              Nov 16 '18 at 18:21











            • Did you copy/paste the code as is? It should work. It was running on my machine. get_file_contents is the function defined on line 7.

              – Jason
              Nov 16 '18 at 19:23











            • I copied and pasted. I can't figure out why I am getting that error.

              – Anna Hamelin
              Nov 16 '18 at 19:51











            • Guess I had it commented it when I ran it before. Moved scoreslist below the function declaration and fixed it. Should work for you now if you copy / paste it. Sorry about that.

              – Jason
              Nov 16 '18 at 20:08











            • This worked perfectly, thank you so so much for your help!!!

              – Anna Hamelin
              Nov 16 '18 at 20:36














            0












            0








            0







            I added a couple of functions to reduce code reuse. Also I load the usernames and passwords in the login_user function so it always gets the latest copy.



            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()





            share|improve this answer















            I added a couple of functions to reduce code reuse. Also I load the usernames and passwords in the login_user function so it always gets the latest copy.



            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()






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 16 '18 at 20:07

























            answered Nov 16 '18 at 18:16









            JasonJason

            3804




            3804













            • This is so helpful! I got a NameError: name 'get_file_contents' is not defined. How can I fix it?

              – Anna Hamelin
              Nov 16 '18 at 18:21











            • Did you copy/paste the code as is? It should work. It was running on my machine. get_file_contents is the function defined on line 7.

              – Jason
              Nov 16 '18 at 19:23











            • I copied and pasted. I can't figure out why I am getting that error.

              – Anna Hamelin
              Nov 16 '18 at 19:51











            • Guess I had it commented it when I ran it before. Moved scoreslist below the function declaration and fixed it. Should work for you now if you copy / paste it. Sorry about that.

              – Jason
              Nov 16 '18 at 20:08











            • This worked perfectly, thank you so so much for your help!!!

              – Anna Hamelin
              Nov 16 '18 at 20:36



















            • This is so helpful! I got a NameError: name 'get_file_contents' is not defined. How can I fix it?

              – Anna Hamelin
              Nov 16 '18 at 18:21











            • Did you copy/paste the code as is? It should work. It was running on my machine. get_file_contents is the function defined on line 7.

              – Jason
              Nov 16 '18 at 19:23











            • I copied and pasted. I can't figure out why I am getting that error.

              – Anna Hamelin
              Nov 16 '18 at 19:51











            • Guess I had it commented it when I ran it before. Moved scoreslist below the function declaration and fixed it. Should work for you now if you copy / paste it. Sorry about that.

              – Jason
              Nov 16 '18 at 20:08











            • This worked perfectly, thank you so so much for your help!!!

              – Anna Hamelin
              Nov 16 '18 at 20:36

















            This is so helpful! I got a NameError: name 'get_file_contents' is not defined. How can I fix it?

            – Anna Hamelin
            Nov 16 '18 at 18:21





            This is so helpful! I got a NameError: name 'get_file_contents' is not defined. How can I fix it?

            – Anna Hamelin
            Nov 16 '18 at 18:21













            Did you copy/paste the code as is? It should work. It was running on my machine. get_file_contents is the function defined on line 7.

            – Jason
            Nov 16 '18 at 19:23





            Did you copy/paste the code as is? It should work. It was running on my machine. get_file_contents is the function defined on line 7.

            – Jason
            Nov 16 '18 at 19:23













            I copied and pasted. I can't figure out why I am getting that error.

            – Anna Hamelin
            Nov 16 '18 at 19:51





            I copied and pasted. I can't figure out why I am getting that error.

            – Anna Hamelin
            Nov 16 '18 at 19:51













            Guess I had it commented it when I ran it before. Moved scoreslist below the function declaration and fixed it. Should work for you now if you copy / paste it. Sorry about that.

            – Jason
            Nov 16 '18 at 20:08





            Guess I had it commented it when I ran it before. Moved scoreslist below the function declaration and fixed it. Should work for you now if you copy / paste it. Sorry about that.

            – Jason
            Nov 16 '18 at 20:08













            This worked perfectly, thank you so so much for your help!!!

            – Anna Hamelin
            Nov 16 '18 at 20:36





            This worked perfectly, thank you so so much for your help!!!

            – Anna Hamelin
            Nov 16 '18 at 20:36


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53342755%2fhow-to-make-username-update-immediately-rather-than-re-running-program%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python