Search code examples
pythonloopsdictionarybooleanbinance

if statement with boolean within loop: 'NoneType' error


I've been searching on stackoverflow and find many posts talking about the error, I've tried many of the solutions presented but none work. The problem looks so simple, I'm really confused what's wrong.

I do an API call to Binance, the output is a long dictionary. I get the key that interests me and it returns either True or False. When i test for type, it shows it's a boolean. I simply run a loop for different symbols in a list and test if the value is true, if so append the symbol name to a new list

test = ['BTCUSDT', 'ETHBTC', 'ATXETH', 'BATETH']
tickers = []
for x in test:
    info = client.get_symbol_info(x)
    a = info['isMarginTradingAllowed']
    if a:
        tickers.append(x)

TypeError: 'NoneType' object is not subscriptable

Any help greatly appreciated. here is the output of info

 {'symbol': 'BTCUSDT',
 'status': 'TRADING',
 'baseAsset': 'BTC',
 'baseAssetPrecision': 8,
 'quoteAsset': 'USDT',
 'quotePrecision': 8,
 'baseCommissionPrecision': 8,
 'quoteCommissionPrecision': 8,
 'orderTypes': ['LIMIT',
  'LIMIT_MAKER',
  'MARKET',
  'STOP_LOSS_LIMIT',
  'TAKE_PROFIT_LIMIT'],
 'icebergAllowed': True,
 'ocoAllowed': True,
 'quoteOrderQtyMarketAllowed': True,
 'isSpotTradingAllowed': True,
 'isMarginTradingAllowed': True}

Solution

  • Your error is in info variable, is None for one x in test list

    This check fix the problem:

    test = ['BTCUSDT', 'ETHBTC', 'ATXETH', 'BATETH']
    tickers = []
    for x in test:
        info = client.get_symbol_info(x)
        if info is None: continue
        a = info['isMarginTradingAllowed']
        if a:
            tickers.append(x)