-1

So after I login successfully in the account the code should stop running, but I don't know why that doesn't happened, it also should happened when i press "q". Also when I go to the second option(register) after I enter the username and then password, they don't instantly get written in the .txt file, but it gets written after I somehow end the code. Is there a way I can write the username and password immediately I write them and then in the same loop login?

Code:

database = open("database.txt", "r+")
status = True


def display_menu():
    print("Login or Register?")
    print("INFO: Press 1 for Login or 2 for Register\nPress q to exit!")
    choice = input("Login/Register/Exit: ")

    if choice == "1":
        login()
    elif choice == "2":
        register()
    elif choice == "q":
        status = False


def login():
    username = input("Enter your name: ")
    password = input("Enter your password: ")

    if username and password in database:
        print("You have successfully login!")
        status = False
    else:
        if password not in database and username in database:
            print("Wrong password!")
        elif username not in database and password in database:
            print("Wrong username!")
        else:
            print("You don't have an account!")


def register():
    create_username = input("Username: ")
    database.write(str(create_username))

    create_password = input("Password: ")
    database.write(str(create_password))

    confirmation_password = input("Confirmation Password: ")

    if create_username in database:
        print("The name is already taken")

    elif confirmation_password != create_password:
        print("Your passwords doesn't match!")
        register()
    else:
        print("You have successfully created your account!\n")
        print("Now you can log in!\n")
        login()

while True:
    display_menu()
R0L3eX
  • 31
  • 3
  • `status = False` is a new local variable. Also you're not looping based on status. Also see https://stackoverflow.com/questions/8641008/compare-multiple-variables-to-the-same-value-in-if-in-python. – jonrsharpe Jan 25 '20 at 20:20
  • `while True:`, I believe it should be `while status:` instead. Also, you're trying to modify a global variable(`status`) inside a function. So, you need to tell python first that it's a global variable. Read https://stackoverflow.com/questions/423379/using-global-variables-in-a-function – Ajay Dabas Jan 25 '20 at 20:23
  • Don't use recursion to implement a loop. Use a `while` loop in `recursion` instead of calling it recursively. – chepner Jan 25 '20 at 20:39
  • You should use a context manager to handle file objects. By the way, why the `str()` in `create_username = input("Username: "); database.write(str(create_username))`? What do you think the return type of `input()` is? – AMC Jan 25 '20 at 20:44

1 Answers1

0

1) if username and password in database: - This code looks for username to be a boolean (True or False), but if it is not, as long as username exists, that part will return True. You need: if username and password in database: Having looked at your code, this is unlikely to work as you expect, due to the way you are storing the data, but it will at least be more correct.

2) You define and edit status multiple times, but never use it in a loop. I assume you want this to be your while condition? Second-last line, you'll want while Status:, not while True

3) Files often only get written to after you do file.close(). So make sure you do that every time you attempt to write to the file, then reopen it.

There's a lot of issues with your code, but hopefully that clears up some of the basic logic errors.

Fwinter1
  • 92
  • 1
  • 1
  • 8