【问题标题】:The else statement is connecting to my other elif statement (python)else 语句连接到我的另一个 elif 语句(python)
【发布时间】:2019-01-17 06:52:53
【问题描述】:
import random    
import asyncio    
import aiohttp
import discord
import json
from discord import Game
from discord.ext.commands import Bot

TOKEN = ''

client = discord.Client()

botnum = 0

@client.event
    async def on_ready():
    print('Online and Ready to Play!')
    print(client.user.name)
    print(client.user.id)
    await client.change_presence(game=discord.Game(name="With Emotions"))
    print('------')



@client.event
async def on_message(message):
    global botnum
# we do not want the bot to reply to itself
    if message.author == client.user:
        return
elif message.content.startswith('Peribot'): ### Updated to elif which is more acceptable
    msg = "I'm Busy! D:<".format(message)
    await client.send_message(message.channel, msg)
elif botnum == 20: ### forgot a ":" and Updated to elif which is more acceptable
    msg = "You Clods are so loud!".format(message)
    await client.send_message(message.channel, msg)
    botnum = 0 ### you were comparing and not setting botnum to 0
else:
    botnum += 1 ### This will add 1 to the preexisting number of botnum
elif message.content.startswith('info'): ### Updated to elif which is more acceptable
    msg = "Created by Popleoma with the intent of being a fun bot. Commands are Peribot, info, and more to come!".format(message)
    await client.send_message(message.channel, msg)
client.run("")

每当我添加最后一个 elif 语句时,它都会连接到前一个 else 语句。我尝试在它们之间添加输入,但它一直在连接。这是我的文本编辑器输入的 https://imgur.com/a/EwPziWb 。我需要一种不连接的方法,因为当它连接时它不会运行我的代码,它只会使 python 编译器崩溃。

【问题讨论】:

  • “连接”是什么意思?为什么else 后面有elifelif 应该是什么的一部分?
  • 缩进在我看来很可疑。它真的运行了吗?
  • 所有 elif 部分都错误地缩进到最后。
  • 嗯,看看您的编辑器屏幕截图,我认为“连接”只是代码编辑器试图理解您的陈述。最后的 elif 看起来是一个孤儿,实际上它与任何东西都没有联系。应该是一个简单的If吗?
  • 或者也许最后一个 elif 应该在 else 之前而不是在它之后?

标签: python discord discord.py


【解决方案1】:

这可能是因为您没有缩进任何 elif 语句以与 if 语句对齐,这就是 python 知道“连接”语句/代码块的方式。它应该是这样的:

@client.event
async def on_message(message):
    global botnum
# we do not want the bot to reply to itself
    if message.author == client.user:
        return
    elif message.content.startswith('Peribot'): 
        msg = "I'm Busy! D:<".format(message)
        await client.send_message(message.channel, msg)
    elif botnum == 20: 
        msg = "You Clods are so loud!".format(message)
        await client.send_message(message.channel, msg)
        botnum = 0 
    elif message.content.startswith('info'): 
        msg = "Created by Popleoma with the intent of being a fun bot. Commands are Peribot, info, and more to come!".format(message)
        await client.send_message(message.channel, msg)
    else:
        botnum += 1 

client.run("")

除此之外,您在 else 语句之后放置了一个 elif 块,这将不起作用。这是一个关于python缩进的教程以供将来参考: https://docs.python.org/2.0/ref/indentation.html

【讨论】:

  • 也为了将来参考,为了避免被否决,你应该将 python runner 的东西称为编译器:)
  • 我帖子中的缩进与我的代码中的缩进不同,抱歉。我不知道的是,elif 必须先于其他人来。一个后续问题是是否有可能有两个其他陈述?
  • No python 不允许你这样做,因为没有办法知道要执行哪个 else 语句。为什么要两个 else 语句,您可以添加另一个 elif 语句或在 botnum += 1 下的另一个 else 语句中添加您想要的行
  • 啊,我明白了,也谢谢你告诉我编译器叫什么。很抱歉我对这种语言如此无知。
  • 供将来参考,虽然我不介意回答初学者问题,但如果人们还没有决定学习基础知识或阅读 python 文档,那么 SO 上的很多人不喜欢回答问题。这将是一个好地方:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-03
  • 2022-10-01
  • 1970-01-01
  • 2012-09-01
  • 1970-01-01
  • 2022-01-06
  • 1970-01-01
相关资源
最近更新 更多