Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的一个if语句在python中不起作用_Python_List_If Statement_Printing - Fatal编程技术网

我的一个if语句在python中不起作用

我的一个if语句在python中不起作用,python,list,if-statement,printing,Python,List,If Statement,Printing,这是电报机器人的一个简单代码。 我有一个列表,当用户输入“m”时,它的名字被添加到列表中,机器人发布列表, 当用户键入“n”时,名称将从列表中删除并发布列表, 当列表数等于2时,机器人应说“完成”:当列表数等于0时,机器人应说“无人”并发布列表 但当我运行这段代码时,当list等于0时,什么也不会发生 # -*- coding:utf-8 -*- #coding=UTF-8 from telegram.ext import Updater , CommandHandler , Filters

这是电报机器人的一个简单代码。 我有一个列表,当用户输入“m”时,它的名字被添加到列表中,机器人发布列表, 当用户键入“n”时,名称将从列表中删除并发布列表, 当列表数等于2时,机器人应说“完成”:当列表数等于0时,机器人应说“无人”并发布列表

但当我运行这段代码时,当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("BOT TOKEN")

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()

我猜这将解决您的缩进问题:

# -*- 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("BOT TOKEN")

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()

想想执行的顺序

  • 您将列表定义为空
  • 您可以定义更改列表的处理程序函数(但不调用它)
  • 打印有关列表的信息(列表仍然为空)
  • 您可以注册处理程序函数
  • 你开始投票
即,在打印列表之前,您没有更改列表

您需要在函数中移动print语句。我还清理了您的代码,因为它在您的问题中似乎已损坏

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

from telegram.ext import Updater, Filters, MessageHandler

updater = Updater("BOT TOKEN")

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()

你的函数有两个参数,但从未调用过?或者我在这里遗漏了什么。你可以像这样将列表声明为全局的,这样在调用函数后它就不会变成空的

def msg_filter(bot, update):
    global listt

您的格式和缩进在问题的代码位中不正确。请修复它。很抱歉,我不知道如何修复它,这就是为什么我在这里问您,您能帮我纠正缩进吗?如果您可以运行代码,并且它不会引发错误,那么您的缩进在代码中与在SO中的不同。如果您不能运行它,则会出现错误或者,您也应该发布该错误。从您的问题“但是当我运行此代码时,当list等于0时,什么也不会发生!“在我看来,你最初的缩进是正确的。顺便说一句,否决票并不意味着你做了什么坏事。你只是没有问出你应该问的问题。请确切地告诉我们你想知道什么,你的问题在哪里,你尝试了什么,你的预期输出是什么,你当前的输出是什么。如果你有任何错误,请发布同样,没什么不好的,不断地问问题,但要努力:)