我收到的行语句无效,除了:来自此代码......
from .utils.dataIO import fileIO
from .utils import checks
from __main__ import send_cmd_help
from __main__ import settings as bot_settings
import discord
from discord.ext import commands
import aiohttp
import asyncio
import json
import os
class Transformice:
"""Transformice"""
def __init__(self, bot):
self.bot = bot
@checks.is_owner()
@commands.group(name="tfm", pass_context=True, invoke_without_command=True)
async def tfm(self, ctx):
"""Get Transformice Stats"""
await send_cmd_help(ctx)
@checks.is_owner()
@tfm.command(pass_context=True)
async def mouse(self, ctx, *name):
"""Get mouse info"""
if name == ():
mouse = "+".join(name)
link = "http://api.micetigri.fr/json/player/" + mouse
async with aiohttp.get(link) as r:
result = await r.json()
name = result['name']
msg = "**Mouse:** {}".format(name)
await self.bot.say(msg)
except:
await self.bot.say("Invalid username!")
def setup(bot):
n = Transformice(bot)
bot.add_cog(n)
有人可以解释我为什么会收到此错误以及如何解决此错误。我对python中的一些错误以及如何修复它感到困惑。
答案 0 :(得分:2)
except
子句仅在try
块之后才有意义,并且没有。看起来你不是在寻找异常处理,而只是一个else
子句。
无论
try:
code_that_might_fail()
except ValueError:
print("ouch.")
或
if condition:
do_this()
else:
do_that()
答案 1 :(得分:0)
你应该放一个
try-except
阻止在你的代码中。
您仅使用except block
...否try
声明。
try:
if name == ():
mouse = "+".join(name)
link = "http://api.micetigri.fr/json/player/" + mouse
async with aiohttp.get(link) as r:
result = await r.json()
name = result['name']
msg = "**Mouse:** {}".format(name)
await self.bot.say(msg)
except:
await self.bot.say("Invalid username!")
如果错误只是因为except
语法,你应该使用上面的内容。
或者您可能希望使用else:
并使用except