When writing code for application.py ran into the fact that in if I can’t set a specific condition. This problem is related to the fact that I am trying to access the non-existent value of the array I selected from SQL .

I attach the code:

  creator = db.execute("SELECT username AS name FROM history WHERE added=:added", added=added) creator = creator[0]["name"] anName = db.execute("SELECT username FROM history WHERE idea=:idea AND added=:added AND username!=:username AND deleted=:deleted", idea=idea, added=added, username=name, deleted=deleted) yourName = db.execute("SELECT username FROM history WHERE idea=:idea AND type=:type AND files=:files AND description=:description AND user_id=:user_id AND username=:username AND added=:added AND deleted=:deleted", idea=idea, type=type1, files=files, description=description, user_id=session["user_id"], username=name, added=added, deleted=deleted) if not anName and not yourName: ... elif anName != None: if anName[0]["username"] == creator: ... elif yourName != None: if yourName[0]["username"] == creator: ... 

As you can see, the anName and yourName can also be None .

Accordingly, to save the code from an error, I first set anName != None . In this case, the browser should have skipped if , the condition in which is incorrect. However, as I understand it, the browser reads the condition even inside the wrong if and with anName == None , and yourName != None still gives an error

 if yourName[0]["username"] == creator: 

IndexError: list index out of range

Question: Can I limit the browser from reading the conditions, the values ​​in which will be equal to None ?

  • len() never happens None - andreymal
  • @andreymal Thank you! I looked at the help site, but I did not understand the theory correctly. However, the problem remained - Yan Farba 2:51 pm
  • comparison with None never brings to good - eri

1 answer 1

The problem was solved by replacing None with 0 in the if check. Thanks @andreymal for the idea!

  if not anName and not yourName: ... elif len(anName) != 0: if anName[0]["username"] == creator: ... elif len(yourName) != 0: if yourName[0]["username"] == creator: ... 
  • or replace just not len(anName) and not len(yourName) - Twiss
  • @Twiss Yes, that’s possible too. Thanks for the idea! And if you need to check the availability of anName , you need to write if not len(anName) == False ? - Yan Farba
  • just without not writing - Twiss
  • @Twiss Thank you! - Yan Farba