[英]Why does one of my IF statements not work in 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()
請檢查縮進。 我認為您已將“ if len(list)== 0:”放在“ if len(list)== 2:”中
也許我不是在看這個權利,但看起來像您的
if len(list)==0:
嵌套在您的內部
if len(list)==2:
這兩個條件永遠不會同時成立,因此永遠不會進入該條件。
也許應該改為:
if len(list)==0:
enter code here
elif len(list)==2:
enter your code here
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.