0

I am doing a project for school - and as part of it, I need to check if their username is already stored within a text file:

def checkUsername():
    userName = str(input("WHAT IS YOUR NAME?"))
    if userName in usernames.read():
        print("WELCOME BACK" + userName)
        print("LET\'S GET STARTED")
        return False
    else:
        usernames.write(userName)
        print("WELCOME TO THE SYSTEM!")
        return False

Despite my efforts to resolve this issue, I cannot seem to figure it out. Can anyone help?

6
  • why text file ? better to use database Commented Dec 20, 2018 at 17:55
  • 4
    because the homework assignment says it's a text file. He's not making design decisions here. Commented Dec 20, 2018 at 17:56
  • @prashantrana He mentioned this is for school, so he likely hasn't be taught about databases. Commented Dec 20, 2018 at 17:56
  • Why do both if and else branches return False? Commented Dec 20, 2018 at 17:57
  • 1
    Also welcome KingJC to SO. Would you be able to post your whole code, it seems like you are fairly close to figuring this out. Commented Dec 20, 2018 at 17:58

3 Answers 3

2

What you are missing is first opening the file for reading:

def checkUsername():
    userName = str(input("WHAT IS YOUR NAME?"))
    with open("usernames.txt", 'r') as usernames:    
        if userName in usernames.read():
            print("WELCOME BACK" + userName)
            print("LET\'S GET STARTED")
            return False
        else:
            usernames.write(userName)
            print("WELCOME TO THE SYSTEM!")
            return False

with open opens the file at the specified path (change usernames.txt to the full path of the file) and 'r' signifies that the file is to be opened with reading permissions. This is usually advantageous to using python's open() method, which requires you close() the file when you are finished reading it.

Side note: notice you have returned False under both conditions of your function.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I thought it was enough to add it as a list before the function but clearly not.
Happy to help :) Before reading from any file, the file has to be opened at its path. Whether you open the file inside the function or outside doesn't make a difference - it will work either way. You were on the right path!
1

One issue with this function is that usernames is not defined, and the other is that both ends of the if block will return False.

One way you could solve these would be

def checkUsername(usernames_file):
    fp = open(usernames_file, 'r')  # the default mode is 'r', but it's explicit here
    usernames = fp.read()

    userName = str(input("WHAT IS YOUR NAME?"))
    if userName in usernames:
        print("WELCOME BACK" + userName)
        print("LET\'S GET STARTED")
        fp.close()
        return True  # note, not False
    else:
        fp.write(userName)
        print("WELCOME TO THE SYSTEM!")
        fp.close()
        return False

That snippet above is different in a few ways, but it also ignores two likely errors you might also be facing: case sensitivity in inputs (the input(...) line could be whatever the user wants), and line separation in usernames_file. Hopefully this pushes you in the right direction though.

Comments

1

i.e.:

def checkUsername(user):
    if user.strip():
        with open("myfile") as users:
            print(f"WELCOME BACK {user}\nLET'S GET STARTED") if user in users.read() else print(f"WELCOME TO THE SYSTEM!")
    else:
        print("Error: empty username")

user = input("WHAT IS YOUR NAME?")
checkUsername(user)

2 Comments

Thank you for your insight.
You're welcome. If my answer helped you please consider accepting it as the correct answer. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.