尝试中的多个IF语句,除了

时间:2020-02-26 15:36:09

标签: if-statement try-catch

所以我下面的脚本是从websocket中获取实时日期并进行解析。它既可以作为[params] [channel],也可以作为[params] [type]。

从下面的代码中可以看到,具体取决于我解析的代码,并将日期中的值分配给变量。

但是脚本永远不会进入第二条elif语句。即使存在data1 [“ params”] [“ type”] ==“ test_request”。由于某种原因,它并没有移到elif语句上,而是直接通过了。我知道这一点是因为我切换了if语句,并且第一个if语句是唯一运行的语句。

我认为这是由尝试引起的,除了。任何帮助将不胜感激。

      response1 = await websocket.recv()
      data1 = json.loads(response1)

      try:
        if data1["params"]["channel"]:
          if data1["params"]["channel"] == perp_ticker:
            perp_bid_price = data1["params"]["data"]["best_bid_price"]   
            perp_ask_price = data1["params"]["data"]["best_ask_price"]   
          elif data1["params"]["channel"] == far2_ticker:    
            far2_bid_price = data1["params"]["data"]["best_bid_price"]   
            far2_ask_price = data1["params"]["data"]["best_ask_price"]   
          elif data1["params"]["channel"] == far3_ticker:
            far3_bid_price = data1["params"]["data"]["best_bid_price"]   
            far3_ask_price = data1["params"]["data"]["best_ask_price"]   
        elif data1["params"]["type"] == "test_request" :
          await websocket.send(msg_publictest)
          print('response sent to heartbeat')
      except:
        pass

0 个答案:

没有答案