【问题标题】:Bot code error: expected an indented block [duplicate]机器人代码错误:预期缩进块[重复]
【发布时间】:2018-03-20 22:44:50
【问题描述】:

所以我目前有这段代码,它为我的不和谐机器人制作了一个 8ball,我遇到了这个错误,上面写着“缩进错误:预期和缩进块”

这是我正在使用的代码(请参阅https://hastebin.com/isageyoqih.py

import discord
import asyncio
import random
import pickle
import os

client = discord.Client()

@client.event
async def on_ready():
    print('Ready and with')
    print(client.user.name)

@client.event
async def on_message(message):
    if message.content.startswith('_whatcanyoudo?'):
        msg = await client.send_message(message.channel, '```Heres what i can do :```')
        await asyncio.sleep(0.5)
        msg2 = await client.send_message(message.channel, '```For now, i can only do a thing called "flip a coin"```')
        await asyncio.sleep(0.5)
        msg3 = await client.send_message(message.channel, '```Bot powered by Ouindoze™, message will delete in 15 seconds```')
        await asyncio.sleep(15)
        await client.delete_message(msg)
        await client.delete_message(msg2)
        await client.delete_message(msg3)


    elif message.content.startswith('_8ball'):
    8ball = random.choice(['It is certain','As i see it, yes', 'Dont count on it', 'Without a doubt', 'Definitely', 'Very doubtful', 'Outlook not so good', 'My sources say no', 'My reply is no', 'Most likely', 'You may rely on it', 'Ask again later'])
    msg5 = await client.send_message(message.channel, 8ball)

client.run('I obiously won't share the token duh xd')

Here is the error I got.

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:
    elif message.content.startswith('_8ball'):
    8ball = random.choice(['It is certain','As i see it, yes', 'Dont count on it', 'Without a doubt', 'Definitely', 'Very doubtful', 'Outlook not so good', 'My sources say no', 'My reply is no', 'Most likely', 'You may rely on it', 'Ask again later'])
    msg5 = await client.send_message(message.channel, 8ball)
    

    应该是(注意elif之后的缩进)

    elif message.content.startswith('_8ball'):
        8ball = random.choice(['It is certain','As i see it, yes', 'Dont count on it', 'Without a doubt', 'Definitely', 'Very doubtful', 'Outlook not so good', 'My sources say no', 'My reply is no', 'Most likely', 'You may rely on it', 'Ask again later'])
        msg5 = await client.send_message(message.channel, 8ball)
    

    顺便说一句,8ball 是非法变量名,因为变量名必须以 字母下划线符号 开头 (_ )。

    注意: 长列表可以分成多行以提高可读性:

        ball = random.choice([
            'It is certain',
            'As i see it, yes', 
            'Dont count on it', 
            'Without a doubt', 
            'Definitely', 
            'Very doubtful', 
            'Outlook not so good', 
            'My sources say no', 
            'My reply is no', 
            'Most likely', 
            'You may rely on it', 
            'Ask again later'
            ])
    

    【讨论】:

    • 像这样的问题应该被评论并投票关闭作为一个简单的印刷错误。因为这个问题对除了 OP 之外的未来读者没有任何帮助
    • 非常感谢,我已经用了 2 个小时了 xD
    【解决方案2】:
    elif message.content.startswith('_8ball'):
        8ball = random.choice(['It is certain','As i see it, yes', 'Dont count on it', 'Without a doubt', 'Definitely', 'Very doubtful', 'Outlook not so good', 'My sources say no', 'My reply is no', 'Most likely', 'You may rely on it', 'Ask again later'])
        msg5 = await client.send_message(message.channel, 8ball)
    

    这正是错误消息所说的:缩进它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-20
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 2014-12-15
      • 2017-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多