Search code examples
pythontext-filesaccount

New users can't create an account with part of an already existing username (text file problem)


I'm relatively new to python and am trying to create a reliable account creation and storage program. When the user wants to create a new username, the username_storage file is read to see if it already exists for another account.

# Enter and check username
            new_user = input("Username: ")
            if new_user in username_storage:
                print("Sorry, that username is already taken.\n")
            else:
                break

The problem is that whenever the user tries to create a new account using part of an existing username, the program does not allow it because it registers that the new username is inside a pre-made username.

e.g. if "william" is an existing username in the username_storage text file, it doesn't allow the new username "will" or "liam" because those names are inside "william".

Thanks to anyone who may be able to answer this question; if I haven't explained my problem clearly enough, please let me know!


Solution

  • usernames = username_storage.split()
    print(new_user in usernames)
    

    would do what you want assuming you cant have spaces in the username