1

i am creating a system where it checks to see if a user has already claimed a username but when creating the account it asks for the username they want and then sends it back to the option screen to ask if they want to login or create an account.

def user_create():
    f = open("users.txt", "a")
    unique = 0
    name = input("Enter a username: ")
    with open("users.txt", "r") as file:
        file_read = csv.reader(file)
        filerow = 1
        for row in file_read:
            if row[0] == name:
                print("This username is already taken, try another one")
                file.close()
                unique = False
                break
            else:
                unique = True
                filerow = filerow + 1
    if unique == True:
        file.close()
    elif unique == False:
        signin1()
    else:
        pass
    if name.replace(" ", "").isalpha():     
        print("Name is valid")
    else:
        print ("Name is invalid")
        user_create()
    password = input("Enter a password: ")
    f.write(name+","+password+",\n")
    f.close()
    print("Account created")
    signin1()

output:

Enter a username: testuser
1. Login
2. Create
Which one would you like to do? 

It is supposed to ask for the users password next but it seems to just skip half the code..

All login code:

def signin1():
    print ("1. Login")
    print ("2. Create")
    choice = input("Which one would you like to do? ")
    if choice == "1":
        existence = os.path.exists("users.txt")
        if existence == False:
            print("There are no authorised accounts yet created")
            signin1()
        else:
            mainsignin()
    elif choice == "2":
        user_create()
    else:
        print("Invaild choice")
        signin1()

    def user_create():
        f = open("users.txt", "a")
        unique = 0
        name = input("Enter a username: ")
        with open("users.txt", "r") as file:
            file_read = csv.reader(file)
            filerow = 1
            for row in file_read:
                if row[0] == name:
                    print("This username is already taken, try another one")
                    file.close()
                    unique = False
                    break
                else:
                    unique = True
                    filerow = filerow + 1
        if unique == True:
            file.close()
        elif unique == False:
            signin1()
        else:
            pass
        if name.replace(" ", "").isalpha():     
            print("Name is valid")
        else:
            print ("Name is invalid")
            user_create()
        password = input("Enter a password: ")
        f.write(name+","+password+",\n")
        f.close()
        print("Account created")
        signin1()

        def mainsignin():
        f = open("users.txt", "w")
        f.close
        with open("users.txt", "r") as file:
            file_reader = csv.reader(file)
            user_find(file_reader)

def user_find(file):
    user = input("Enter your username: ")
    filerow = 1
    login = False
    for row in file:
        if row[0] == user:
            print("Username found:", user)
            user_found = [row[0], row[1]]
            login = True
            break
        else:
            filerow = filerow + 1
        if login == True:
            global user1
            user1 = user
            pass_check(user_found)
        else:
            print ("Could not find user with the name: "+str(user))
            signin1()

def pass_check(user_found):
    userpass = input("Enter your password: ")
    if user_found[1] == userpass:
        print("password match")
        str(user_found)
        game_login_user2()
    else:
        print("password does not match")
        mainsignin()
  • Looks to me like you're proceeding straight to sign-in on `elif unique == False`, when instead you probably want the user to get an error message. – Blue Star Aug 23 '20 at 21:25
  • 1
    When you open a file in append mode (`'a'`), it seeks to the end automatically. That means there's nothing you can read, so the loop never runs. – Blckknght Aug 23 '20 at 21:36
  • @BlueStar I'm not sure that's the problem because the error message is under 'if row [0] == name' and then it closes the file, then it sets unique to False, then breaks the loop to then lead onto the if statement for the sign in – Andrew Bruce Aug 23 '20 at 21:38
  • @Blckknght That's so if the file isn't created it will create it at the start of the function so the rest of the code can function. Then later in the code it reopens the file where it says 'with open("users.txt", "r") as file' – Andrew Bruce Aug 23 '20 at 21:39
  • It's slightly strange because I don't get an error message – Andrew Bruce Aug 23 '20 at 21:42
  • Oh, I'm wrong, you're opening the file twice at the same time, once in read mode and once in append mode. That's *probably* a bad idea but not obviously the cause of the issue. – Blckknght Aug 24 '20 at 01:43

2 Answers2

1

Your issue is stemming from this condition: elif unique == False:

You first assign the value 0 into unique, then assign a True or a False into it depending on the contents of your file. If your file is empty, then unique will still have a 0 at the end of the file loop. Now, the potentially surprising thing here is that 0 == False is evaluated as true! This is because 0 is "Falsy" (see this question for more info on that). So while you were expecting the code to move on to the else, in fact it goes back to signin1.

To avoid mistakes like this in the future, I highly recommend using x is True and x is False in place of x == True and x == False. I also recommend that you don't use different types (int, bool) in the same variable, as that can lead to mishaps like this one.

Last but not least, I suggest that you learn how to use a debugger. If you were running this code line by line, you would catch this issue pretty fast.

Blue Star
  • 1,932
  • 1
  • 10
  • 11
  • Thank you, I didn't quite understand what happens when a file is empty but your comment helped and the code is now working again – Andrew Bruce Aug 24 '20 at 07:12
0

I'm not sure if I understand your problem correctly, the issue seem to be in user_create function, unique is set to 0 (False) at the very start of this function, if users.txt happen to be empty, signin1 would be called even if a unique username is given.

def user_create():
    f = open("users.txt", "a")
    unique = 0  # unique is set to False
    name = input("Enter a username: ")
    with open("users.txt", "r") as file:
        file_read = csv.reader(file)
        filerow = 1
        for row in file_read: # if users.txt is empty, following 8 lines won't run
            if row[0] == name:
                print("This username is already taken, try another one")
                file.close()
                unique = False
                break
            else:
                unique = True
                filerow = filerow + 1
    if unique == True:
        file.close()
    elif unique == False:
        signin1() # and signin1 would be called here even if a unique username is given
    else:
        pass
yibo
  • 517
  • 3
  • 10