samedi 15 juillet 2017

Python if, elif, else

I am trying to load json-strings that I saved to textfiles back into Python. Since the json-module gives me an error when I give it an empty file, I did the following:

fp = open(filename, 'r')

if fp.readlines() == []
     # return an empty list, since the file is empty
elif fp.readlines() != []
     # return the converted list

Since I now have both if a == b and if a != b, I was very surprised to find that none of the to 'ifs' gets executed, but instead if I add

else:
     print('something')

it actually prints 'something'. Why is this happening? How can there logically exist something, its negation and then something else?

Aucun commentaire:

Enregistrer un commentaire