Home > Back-end >  Error with if statement when checked variable is not defined
Error with if statement when checked variable is not defined

Time:05-13

I have some variables A and B and I want to check them for a value and if both are False, the function shall render True:

if ( A == 1 and B == 2 ) == False :
do magic

I breaks down if either A or B is not defined and when I test it with

print(A == 1 and B == 1)

this breaks with an error:

IndexError: list index out of range

A or B are actually some variables from web-API and this whole thing runs in a loop, constantly checking and can be either set or not set with a value.

Is there a way to ignore whether A or B is defined or yield a default FALSE if not defined/existing?

CodePudding user response:

Thanks heaps to @iBug

My solution is to check if there is anything in the list and then check if there is the desired element in the list:

'Yes' if fruit == 'Apple' else 'No'

client.futures_get_open_orders()[0]['side'] == 'BUY' if `client.futures_get_open_orders() == True else False`

Just had to sleep and drink 3 coffee in succession next morning :D

See: Putting a simple if-then-else statement on one line

  • Related