Thread: Always "False"

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    2
    Rep Power
    0

    Always "False"


    Hi!
    i am trying to make a login page of some kind to help me learn python, but it doesn't work. Where it doesn't work it looks like this:
    Code:
    #Creates a new password
    def ChangePassword():
        global u
        p = input("Old Password: ")
        file = open("Accounts.txt", "r")
        x = file.read()
        file.close()
        x = ast.literal_eval(x)
        print(x)
        x = x.get(u)
        print(x)
        print(u)
        if x == p:
            print("True")
        else:
            print("False")
    the script use a file("Accounts.txt") and that is where the password is coming from. But at "x = x.get(u)" x gets no value. Please help!
    If you are trying this script out, have "{}" in "Accounts.txt", i don't think it works otherwise :P

    The whole script:

    Code:
    import ast
    import sys
    #Welcome Page
    def main():
        print("Welcome to Filips Login!")
        x = input("1. Login \n2. Create Account\n3. Exit\n")
        if x == "1":
            Login()
        elif x == "2":
            Check()
        elif x == "3":
            sys.exit()
        else:
            main()
    
    #Checks if the username key has the right password value
    def Login():
        global u
        u = input("Username: ")
        p = input("Password: ")
        file = open("Accounts.txt", "r")
        x = file.read()
        file.close()
        x = ast.literal_eval(x)
        u = x.get(u)
        if u == p:
            print("Login Successful!")
            Profile()
        else:
            print("Login Failed")
            main()
    
    #Checks if username is already taken
    def Check():
        global u
        u = input("Username: ")
        file = open("Accounts.txt", "r")
        x = file.read()
        file.close()
        y = ast.literal_eval(x)
        if u in y:
            print("Username already taken")
            Check()
        else:
            Create()
    
    #Creates a new user
    def Create():
        global u
        p = input("Password: ")
        file = open("Accounts.txt", "r")
        y = file.read()
        file.close()
        y = ast.literal_eval(y)
        y[u] = p
        file = open("Accounts.txt", "w")
        file.write(str(y))
        file.close()
        print("Account Created!")
        main()
    
    #Opens the profile page
    def Profile():
        print("Welcome, you have logged in!")
        x = input("1. Change Password\n2. Delete Account\n3. Logout\n")
        if x == "1":
            Change()
        elif x == "2":
            DeleteAccount()
        elif x == "3":
            main()
    #Change password page
    
    def Change():
        x = input("1. Change Password\n2. Profile\n")
        if x == "1":
            ChangePassword()
        elif x == "2":
            Profile()
        else:
            print("error")
            Change()
    
    #Creates a new password
    def ChangePassword():
        global u
        p = input("Old Password: ")
        file = open("Accounts.txt", "r")
        x = file.read()
        file.close()
        x = ast.literal_eval(x)
        print(x)
        x = x.get(u)
        print(x)
        print(u)
        if x == p:
            print("True")
        else:
            print("False")
    
    #Deletes the user's account
    def DeleteAccount():
        print("Delete Account")
    
    main()
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    475
    Rep Power
    33
    Assuming each password is a separate line in the file (you didn't say) then take a look at this tutorial on how to read a file. In addition to "print line" in the example, you will compare the password from the input statement and see if any match.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Code:
    #Checks if the username key has the right password value
    def Login():
        global u
        u = input("Username: ")
        p = input("Password: ")
        file = open("Accounts.txt", "r")
        x = file.read()
        file.close()
        x = ast.literal_eval(x)
        u = x.get(u)                # <<<<<<<<---------------Here you've changed u to be the password
        if u == p:
            print("Login Successful!")
            Profile()
        else:
            print("Login Failed")
            main()
    I'll save the lecture about recursion without certain termination for another day. Use a loop around your menu instead of calling main again and again.

    Comments on this post

    • Fillelind agrees
    Last edited by b49P23TIvg; January 6th, 2014 at 11:48 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    2
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Code:
    #Checks if the username key has the right password value
    def Login():
        global u
        u = input("Username: ")
        p = input("Password: ")
        file = open("Accounts.txt", "r")
        x = file.read()
        file.close()
        x = ast.literal_eval(x)
        u = x.get(u)                # <<<<<<<<---------------Here you've changed u to be the password
        if u == p:
            print("Login Successful!")
            Profile()
        else:
            print("Login Failed")
            main()
    I'll save the lecture about recursion without certain termination for another day. Use a loop around your menu instead of calling main again and again.
    Thank you!

    I think i need a lecture about recursion without certain termination. What is it?
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Something like this not fully tested mash style:
    Code:
    import ast
    import sys
    
    DATABASE_NAME = 'Accounts.txt'
    
    # abhor duplicate code.  Wrap reuseable code in functions.
    def load(name=DATABASE_NAME):
        with open(name) as inf:
            a = inf.read()
        return ast.literal_eval(a)
        
    def save(s, name=DATABASE_NAME):
        with open(name, "w") as ouf:
            ouf.write(str(s))
    
    
    #Welcome Page
    def main():
        while True:         # use a loop instead of recursion back to main
            print("Welcome to Filips Login!")
            x = input("1. Login \n2. Create Account\n3. Exit\n")
            if x == "1":
                Login()
            elif x == "2":
                Check()
            elif x == "3":
                sys.exit()
    
    #Checks if the username key has the right password value
    def Login():
        u = input("Username: ")
        p = input("Password: ")
        x = load()
        if x.get(u) != p:
            print("Login Failed")
        else:
            print("Login Successful!")
            Profile(u)
    
    #Checks if username is already taken
    def Check():
        # open and evaluate the file just once.
        y = load()
        while True:
            u = input("Username: ")
            if u in y:
                print("Username already taken")
            else:
                Create(u)
                return
    
    #Creates a new user
    def Create(u):
        p = input("Password: ")
        y = load()
        y[u] = p
        save(y)
        print("Account Created!")
    
    #Opens the profile page
    def Profile(username):
        print("Welcome, you have logged in!")
        while True:
            x = input("1. Change Password\n2. Delete Account\n3. Logout\n")
            if x == "1":
                Change(username)
            elif x == "3":
                return
            elif x == "2":
                DeleteAccount(username)
                return
    
    #Change password page
    def Change(username):
        while True:
            x = input("1. Change Password\n2. Profile\n")
            if x == "1":
                ChangePassword(username)
            elif x == "2":
                return
            else:
                print("error")
    
    #Creates a new password
    def ChangePassword(username):
        p = input("Old Password: ")
        accounts = load()
        if accounts[username] != p:
            print('password not changed')
        else:
            x[username] = input('New password: ')
            save(accounts)
            print('password changed')
    
    #Deletes the user's account
    def DeleteAccount(username):
        db = DATABASE_NAME
        print("Delete Account")
        accounts = load(db)
        del accounts[username]
        save(accounts, db)
    
    main()
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo