For some reason , my code keeps returning a false reply even if the string is in the file. Here's my code:

def check(self):
    self = str(self)
    if self in config_file:
        print ('Account exists')
    if self not in config_file:
        print ('Account doesn\'t exist')

Recommended Answers

All 2 Replies

You can use


instead of second if at line 5, otherwise correct, even the whole function seems pointless as it is only

print 'Account' + ('exists' if str(myobj) in config_file else "doesn't exist")

Where myobj is of type of the your class definition. I would at least define only

def check(self):
    return str(self) in self.config

And use that in my checks instead of the in conditions, and the self.config contains the read in string of the config file (if it would be file object you could use it only once as the file pointer would move as side effect of the test)

Do you know about ?

What is the type of the variable config_file ?

commented: A string. +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.