Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.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/0/search/2.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
Python 来自except的语法无效_Python - Fatal编程技术网

Python 来自except的语法无效

Python 来自except的语法无效,python,Python,我收到一个无效的语法,从这行说,除了:从这个代码 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

我收到一个无效的语法,从这行说,除了:从这个代码

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中的一些错误以及如何修复这些错误感到困惑。

只有在try块之后,except子句才有意义,而且没有。看起来您并不是在寻找异常处理,而是在寻找一个else子句

或者

try:
    code_that_might_fail()
except ValueError:
    print("ouch.")

你应该放一个

尝试将块放在一起,但不在代码中。 您只使用了除块之外的块。。。没有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

在文档中您在哪里找到if/except construct?
   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!")