[英]SyntaxError discord.py
I'm new to coding and I'm creating a Discord bot and I still get a Syntax error, here is the code:我是编码新手,我正在创建一个 Discord 机器人,但仍然出现语法错误,这是代码:
import discord
import os
from discord.ext import commands
client = commands.Bot(command_prefix = "/")
@client.event
async def on_ready():
print("Bot is online!")
for filename in os.listdir("./cogs"):
if filename.endswith(".py"):
try:
client.load_extension(f"cogs.{filename[:-3]}")
print(f"Loaded {filename}")
except Exception as e:
print(f"Failed to load {filename}")
print(f"[ERROR] {e}")
client.run("token")
And this is the error这就是错误
File "main.py", line 15
except: Exception as e:
^
SyntaxError: invalid syntax
try:
client.load_extension(f"cogs.{filename[:-3]}")
print(f"Loaded {filename}")
except Exception as e:
print(f"Failed to load {filename}")
print(f"[ERROR] {e}")
The except is supposeod to be outside the try chunk. except 应该在 try 块之外。 The except cannot find any try in the scope similarly the try doesnt find any except in its scope除了在 scope 中找不到任何尝试,类似地,尝试在其 scope 中找不到任何尝试
The indentation can differ from ide to ide缩进可以从 ide 到 ide 不同
This might happen because Python indentation is not done right.这可能是因为Python 缩进没有正确完成。 Your try
and except
is has a different indentation, make sure the except
statement is at the same level as the try
one.您的try
和except
具有不同的缩进,请确保except
语句与try
语句处于同一级别。
Corrected code:更正的代码:
import discord
import os
from discord.ext import commands
client = commands.Bot(command_prefix = "/")
@client.event
async def on_ready():
print("Bot is online!")
for filename in os.listdir("./cogs"):
if filename.endswith(".py"):
try:
client.load_extension(f"cogs.{filename[:-3]}")
print(f"Loaded {filename}")
except Exception as e:
print(f"Failed to load {filename}")
print(f"[ERROR] {e}")
client.run("token")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.