0

I want my function to return True or False but it always return False. I've tried to remove the first assignment of user(user = False) but an error occured : "user is not defined"

Here is the function

def findUser(name):
    user = False
    with open('users', 'rb') as usersFile:
        myUnpickle = pickle.Unpickler(usersFile)
        users = myUnpickle.load()
        for userName, userScore in users.items():
            if(userName.lower() == name.lower()):
                user = True
    return user

The file content is a dictionary When I print users, I see them but when I try to find a user using the function I get a False

With this I expected to get a "True" If data's content is {"Jean":20, "Joe":10}

def findUser(name):
    user = False
    with open('data', 'rb') as usersFile:
        myUnpickle = pickle.Unpickler(usersFile)
        users = myUnpickle.load()
        for userName, userScore in users.items():
            if(userName.lower() == name.lower()):
                user = True
    return user

if(findUser("Joe")):
    ...
else
    ...

But all I have is the else block

Here is the insertion

def insertUser(name)
    If(os.path.exists("users")):
        with open('users', 'rb') as usersFile:
            myUnpickler = pickle.Unpickler(usersFile)
            fileContent = myUnpickler.load()
            fileContent[name] = 0
        with open('users', 'wb') as usersFile:
            myPickler = pickle.Pickler(usersFile)
            myPickler.dump(fileContent)
    else:
        with open('users', 'rb') as usersFile:
            default = {"default":0}
            pickle.Pickler(usersFile).dump(default)
  • Can you share the code you used to pickle/save the file? Are you sure it contains the content that you think it does? – dskrypa Nov 19 '22 at 19:12
  • 2
    Try printing `userName` and `userScore` or just `users` if the file is relatively small. – ddejohn Nov 19 '22 at 19:12
  • 1
    The only answer is that `userName.lower() == name.lower()` isn't ever true. – John Gordon Nov 19 '22 at 19:16
  • @dskrypa I have added the insertion code – Guiven Pambou Nov 19 '22 at 19:48
  • @ddejohn, I did it and it was as expected – Guiven Pambou Nov 19 '22 at 20:03
  • Based on the code you pasted as an answer, your file will only ever contain `{'default': 0}` - it will never have the name you are looking for (unless that name is `'default'`) – dskrypa Nov 19 '22 at 20:12
  • One more thing that I don't understand, when I remove the first user = False at the beginning and add an else with user = False I have an UnboundLocalError: local variable user referenced before assignment. If it is not true then it's false, I have the 2 cases logically user might exist – Guiven Pambou Nov 19 '22 at 20:15
  • Regarding scope: https://stackoverflow.com/questions/57797690/variable-scope-and-name-resolution-in-python https://stackoverflow.com/questions/291978/short-description-of-the-scoping-rules https://realpython.com/python-namespaces-scope/ – dskrypa Nov 19 '22 at 20:18
  • @dskrypa but when I open the file, there are all the users, I've even tried with "default" but it didn't work – Guiven Pambou Nov 19 '22 at 20:19
  • Looking at the `insertUser` in the answer again, you're opening the file for reading (`rb`) not writing (`wb`) for the dump. – dskrypa Nov 19 '22 at 20:22
  • As written, `findUser` opens the file `'data'`, while `insertUser` opens the file `'users'`. Can you verify for us you're opening the right file? – Jasmijn Nov 19 '22 at 20:33
  • @Jasmijn. I was opening the wrong file – Guiven Pambou Nov 19 '22 at 22:32
  • @GuivenPambou if you've found that the problem is a simple mistake and you don't require an answer, you should close the question. You can also ask someone to write a formal answer if you think it would be of benefit to others (and then accept it), or write your own answer and accept that, if you want. You shouldn't leave the question open and unanswered though. – Grismar Nov 20 '22 at 05:40

1 Answers1

0

I was just opening the wrong file when looking for the user, I didn't add what to do if the file wasn't find that's why when there was not an initialization of the return variable I got an error. I had just to change the file name. The variable's value inside the "with" wasn't take into account because the "with" block was never called, the condition to go there was always false. The file name was "users" but my code was :

Def connectUser(userName):
    user = False
    if(os.path.exists("user")): //user Without "s"
        with:
            ...
            user = True
    return user

The file doesn't exist, the code inside the if doesn't exist

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 24 '22 at 05:40