1

I'm trying to get my code to check if a word is already in the document. However when choosing a variable (username) that happens to share the same letters going to the right as the preexisting one in the file, it thinks that the name is taken. For example, if abcdefg was in the file, if I was to right defg or fg or g, it would think the username was taken.

def register():
    print("━━━━ACCOUNT CREATION━━━━")
    username = input("Create Username: ")
    with open("Login.txt", "r") as loginfile:
        if (username+",") in loginfile.read():
            print("Sorry, but that username is taken.")
            choice = input("Try again with a new name? (Y/N)")
            choice = choice.upper()

My case: Say I had the name, Joe which is already in the file. If I tried to make a username that is just e, then it would think it is Joe, as it is looking for the e, next to a comma.

Anyway to fix this? Thanks!

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Fiery
  • 11
  • 4

2 Answers2

0

This should work

with open('login.txt', 'r') as LoginFile:
    # the split function splits a string to a list on mark
    Data = LoginFile.read().split(" ,") 
    if username in Data:
        # .....

if this isn't what you want try this built-in module :

https://docs.python.org/3/library/re.html

12ksins
  • 307
  • 1
  • 12
  • Thanks for the reply, this worked except it then made it so there could be two accounts with the same username. Thanks for the contribution anyway though! – Fiery Jul 07 '20 at 19:54
0
def register():
    print("━━━━ACCOUNT CREATION━━━━")

    # read the names from the file
    with open('Login.txt', 'r') as f:
        names = f.read().split(',')

    username = input("Create Username: ")

    for name in names:
        # check if any names end with this name have been created
        if name.endswith(username):
            # found
            print("Sorry, but that username is taken.")

            # we want to keep ask the user to select if
            # they enter something other than Y/N
            while True:
                # ask for the option
                option = input("Try again with a new name? (Y/N) ")
                # try again, we just rerun this function
                if option == 'Y':
                    register()
                    # don't ask any more
                    break
                elif option == 'N':
                    # exit if user chooses N
                    break
                # if the user chooses something else, continue
                # the loop and keep asking
                
            # if no names end with username, goto else
            break
    else:
        # name available, save it to the file
        print("Name created successfully:", username)
        new_names = names + [username]
        with open('Login.txt', 'w') as f:
            f.write(','.join(new_names))

I have tested it, please try and see if it works for you.

Panwen Wang
  • 3,573
  • 1
  • 18
  • 39
  • I think this works. However I am unfortunately not smart enough to implement this. I will mark as correct anyway though. – Fiery Jul 07 '20 at 19:47
  • @Fiery No problem. Just try and test it out. Feel free to let me know if you have any questions. – Panwen Wang Jul 07 '20 at 19:49
  • i have just tried it and it didn't work :(. I set the name as joe, the next time i tried it, i put the name as e, it still said it was taken :O. do you know how to fix this, or is it something to do with (r'\b([\w\d_]*' + username + r) because idk what those mean lol. Thanks! – Fiery Jul 07 '20 at 20:06
  • Did you also want to save the name created by the user into the `Login.txt`, so that next time it should be reported as being taken? Or could you elaborate aims, step by step? For example, what are the behaviors when you input different names? I was actually a little bit confused. – Panwen Wang Jul 07 '20 at 20:09
  • Thanks for your responses! I can already save them to the file for next time, however the bit I am stuck at is the checking for the variable. This is is because the way I go about it is by checking if Username is next to the comma. This is because that is the layout of the txt document. My problem is, it checks if it is next to a comma, however when it checks if the username is next to a comma, if it is the end of another username, then it will think it is in the file and say the username is taken. – Fiery Jul 07 '20 at 20:20
  • Let me rephrase: suppose your file is empty at the beginning, if I input a name `Joe`, then it can be successfully created and saved into the file as `Joe,`. Next time, if I input `Sue`, it should say `name being taken`, since you said, the last character `e` is in the file and `,` is next to it. Am I understanding correctly? – Panwen Wang Jul 07 '20 at 20:32
  • It would still work for sue, however `oe` would not work due to it being the end of the word. I find it very confusing. – Fiery Jul 07 '20 at 20:36
  • I see, so if I have a long name, say `abcdefg`, any right-most substring won't work, right? For example: `bcdefg`, `defg`, etc. – Panwen Wang Jul 07 '20 at 20:38
  • Gotcha. Let me modify my answer then. You'd better also rephrase your question as well to make it less confusing. – Panwen Wang Jul 07 '20 at 20:40
  • Thanks a bunch, I have changed my question to better match what I am asking for. – Fiery Jul 07 '20 at 20:48
  • I have tried it. Still not working for me :( are you sure it worked? I entered the name as `Fiery`, then tried making another account called `ery`, it said it was taken. Thanks! – Fiery Jul 07 '20 at 21:08
  • Isn't that intended? `Fiery` ends with `ery`. – Panwen Wang Jul 07 '20 at 21:20
  • You mentioned in your main post "For example, if abcdefg was in the file, if I was to right defg or fg or g, it would think the username was taken." – Panwen Wang Jul 07 '20 at 21:21
  • Yes, that is what i don't want to happen. I want it so if i create an account called `abcdefg`,i can still create an account called `defg` or `fg` or `g`. – Fiery Jul 07 '20 at 21:32
  • Then what should be reported being taken? – Panwen Wang Jul 07 '20 at 21:37
  • `abcdefg` should be taken. However nothing else like `defg`. – Fiery Jul 08 '20 at 08:10
  • UPDATE: I have fixed the problem by putting a ' just before the username.This fixes it as it means that it will only think the name is taken IF the whole word is next to the apostrophe. – Fiery Jul 08 '20 at 10:45
  • Glad you fixed it. – Panwen Wang Jul 08 '20 at 16:25