Search code examples
pythonoopdecorator

How to check if the line is equal to my specific one?


I made a function that checks if there is a string in the file and compares it with my 'password', if they match, I raise an exception:

@staticmethod       
    def dict_check(password):
        with open('dict.txt', 'r') as f:
            for line in f.readlines():
                if line == password:
                    return True
            return False

after that I put my func in my @property.setter:

if not User.dict_check(value):
    raise ValueError("...")

When I put password in my instance I always have the same issue:

---> 51     raise ValueError("...")
     52 self.__password = value

ValueError: ...

Even though my password is different from the string from the .txt file.

Sorry Im like very new in IT, so


Solution

  • The not operator creates a double negative. So the code you wrote will trigger an exception only if the password does not exist in the .txt file. Either remove the not or switch the return values of the method to fix.