【问题标题】:why does my if statement not work in python?为什么我的 if 语句在 python 中不起作用?
【发布时间】:2019-07-20 09:39:29
【问题描述】:

这是电报机器人的简单代码。 我有一个清单, 当用户键入“m”时,它的名字被添加到列表中,机器人发布列表 当用户键入“n”时,该名称从列表中删除并发布列表 当列表的数量等于 2 时,机器人应该说“完成:并且没有收到其他金额 当列表的数量等于 0 时,机器人应该说“No one”并发布列表。

但是当我运行这段代码时,当 list 等于 0 时没有任何反应!

# -*- coding:utf-8 -*-
#coding=UTF-8

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

updater = Updater("898399795:AAEI-HlZ2EIMgB-DRAhyqqm6k4NeSoeohhM")

listt = []


def msg_filter(bot , update):
    wordsp = ['m']
    wordsn = ['n']



    if any (i in update.message.text for i in wordsp) and " 
    {}".format(update.message.from_user.first_name) not in listt:

    listt.append("{}".format(update.message.from_user.first_name))
    bot.send_message(chat_id = update.message.chat_id , text = 
    "\n".join(listt))
    bot.send_message(chat_id = update.message.chat_id , text = len(listt))

       if len(listt)==2:
           bot.send_message(chat_id = update.message.chat_id , text = 
           "Done")

    if any (i in update.message.text for i in wordsn) and " 
    {}".format(update.message.from_user.first_name)  in listt:


    listt.remove("{}".format(update.message.from_user.first_name))
    bot.send_message(chat_id = update.message.chat_id , text = 
    "\n".join(listt))

    bot.send_message(chat_id = update.message.chat_id , text = len(listt))

       if len(listt)==0:
       bot.send_message(chat_id = update.message.chat_id , text = 
       "nobody")



print(listt)
print(len(listt))

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

【问题讨论】:

  • 请修正代码的缩进。

标签: python list if-statement printing


【解决方案1】:

我看你的情况不对

 if len(listt) == 2

应该是

 if len(listt) == 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    相关资源
    最近更新 更多