首页 > 解决方案 > 为什么我的 IF 语句之一在 Python 中不起作用?

问题描述

这是我的代码,除了最后一个 if 语句外,它运行良好。

我想当列表成员变为 0 时,机器人发送此消息:

"No One"

当列表达到 2 机器人说:修复时,此代码完美运行。

但是当它达到0时,它什么也没说!

 from telegram.ext import Updater , CommandHandler , Filters , 
 CommandHandler , MessageHandler
 from telegram import MessageEntity
 from telegram import ParseMode , InputTextMessageContent

 updater = Updater("989165404:AAF8DEjyunwrb88-1G8w62cGItzXj1J618g")

list = []
wordsm=["m"]
wordsn=["n"]

def msg_filter(bot , update):

    if any (i in update.message.text for i in wordsm):
        list.append("{}".format(update.message.from_user.first_name))
        bot.send_message(chat_id = update.message.chat_id , text = 
        "\n".join(list))

       if len(list)==2:
        bot.send_message(chat_id = update.message.chat_id , text = "FiX!")


         if any (j in update.message.text for j in wordsn):
         list.remove("{}".format(update.message.from_user.first_name))
          bot.send_message(chat_id = update.message.chat_id , text = 
         "\n".join(list))


         if len(list)==0:
        bot.send_message(chat_id = update.message.chat_id , text = "No 
        One")

       print(list)
       print(len(list))

       updater.dispatcher.add_handler(MessageHandler(Filters.text 
       ,msg_filter ))
       updater.start_polling()

标签: pythonlistif-statementprintinglogic

解决方案


请检查缩进。我认为您已将“if len(list)==0:”放入“if len(list)==2:”中


推荐阅读