-3

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?

nolimits
  • 43
  • 1
  • 3
  • 16
  • 1
    Normally it's a bad design to check for "definition of variables". You should refactor your code so that it doesn't depend on this kind of things. – iBug May 10 '22 at 05:53
  • 5
    Also please post a [mcve]. I cannot see how your print statement produces `IndexError`. There must be a list somewhere that you did not show us. – iBug May 10 '22 at 05:53
  • This question may be from interest, as it presents multiple ways of checking if a variable is defined: https://stackoverflow.com/questions/843277/how-do-i-check-if-a-variable-exists – Mime May 10 '22 at 05:54
  • What do you mean "if both are false"? Do you mean if both are 0? – Sayse May 10 '22 at 05:58
  • You should have your variables initialized, perhaps to `None`: then simply `if a:` will be `False` if `a` is still `None`, `True` otherwise – gimix May 10 '22 at 06:18
  • Thanks @iBug It is supposed to be a list. Didn't think that might cause the error. Do you have a suggestions how to "unlist" this and bypass the error? – nolimits May 11 '22 at 01:16
  • 1
    There's nothing we can help with until you provide a [mcve]. Show your code, please. – iBug May 11 '22 at 03:28
  • Thanks @iBug I understand the problem much better now. The list print(A[0]['Z'] == "ABC") I request is not existent. Not sure how to solve it though. It's technically about "print( (client.futures_get_open_orders()[0]['side'] == 'BUY'))", but I not sure if this confuses more (you also need to provide API keys for that, which I rather not want to)? – nolimits May 11 '22 at 09:29
  • 1
    A [mcve] need not be your actual code. It is (preferably) a stripped-down version of the offending code that is *just enough to reproduce the problem*. Also, please [edit] your question to include any necessary information instead of commenting. – iBug May 11 '22 at 10:03
  • 1
    For example, the whole API calling part may be irrelevant to your problem, and you can replace it with its return value (which is supposedly an empty list, `A = []`) in your "code sample". – iBug May 11 '22 at 10:04
  • 1
    And if you do that with an empty list, you may have known where the problem lies. This is how debugging by yourself helps you understand the code. If you still don't, proceed to [edit]ing your question for better details. – iBug May 11 '22 at 10:06

1 Answers1

0

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

nolimits
  • 43
  • 1
  • 3
  • 16