首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我在python编码中遇到了缩进问题,但是我找不到确切的问题来修复它。

我在python编码中遇到了缩进问题,但是我找不到确切的问题来修复它。
EN

Stack Overflow用户
提问于 2019-05-18 03:29:28
回答 2查看 1.8K关注 0票数 0

Unindent does not match any outer indentation level (<unknown>), line 47) pylint(syntax-error) [47,1]

代码:

代码语言:javascript
复制
def takeCommand():
    #It takes mircrophone input from the user and return string output

    r = sr.Recognition()
    with sr.mircrophone() as source:
        print("Listening...")
        r.pause_threshold = 1
        audio = r.listen(source)

    try:
        print("Recognizing...")
        query = r.recognize_google(audio, language= 'eng-ben')
        print(f"User said: {query}\n")

    except Exception as e:                  (the problem is in this line)
        #print(e)
         print("Pardon me? Would be able to repeat that for me?")
         return "None"
    return query

来创建一个虚拟助手。

EN

回答 2

Stack Overflow用户

发布于 2019-05-18 03:36:02

请检查下面代码中注释部分下面的行。您可以将注释(#)下面的行移回1个空格吗?

except Exception as e: (the problem is in this line) #print(e) <-print("Pardon me? Would be able to repeat that for me?") <-return "None"

票数 0
EN

Stack Overflow用户

发布于 2019-05-18 03:41:20

通过pylint运行您的代码(在注释掉"problem here“注释之后),我得到了以下结果:

代码语言:javascript
复制
$ pylint doit2.py 
************* Module doit2
doit2.py:12:50: C0326: No space allowed after keyword argument assignment
        query = r.recognize_google(audio, language= 'eng-ben')
                                                  ^ (bad-whitespace)
doit2.py:16:17: C0303: Trailing whitespace (trailing-whitespace)
doit2.py:17:65: C0303: Trailing whitespace (trailing-whitespace)
doit2.py:17:0: W0311: Bad indentation. Found 9 spaces, expected 8 (bad-indentation)
doit2.py:18:0: W0311: Bad indentation. Found 9 spaces, expected 8 (bad-indentation)
doit2.py:1:0: C0111: Missing module docstring (missing-docstring)
doit2.py:1:0: C0103: Function name "takeCommand" doesn't conform to snake_case naming style (invalid-name)
doit2.py:1:0: C0111: Missing function docstring (missing-docstring)
doit2.py:4:4: C0103: Variable name "r" doesn't conform to snake_case naming style (invalid-name)
doit2.py:4:8: E0602: Undefined variable 'sr' (undefined-variable)
doit2.py:5:9: E0602: Undefined variable 'sr' (undefined-variable)
doit2.py:15:11: W0703: Catching too general exception Exception (broad-except)
doit2.py:15:4: C0103: Variable name "e" doesn't conform to snake_case naming style (invalid-name)
doit2.py:15:4: W0612: Unused variable 'e' (unused-variable)

------------------------------------
Your code has been rated at -5.71/10

我假设您的问题是关于由python linter生成的W0311: Bad indentation注释。也就是说,您需要在捕获异常块内的每个printreturn语句前删除一个空格。

另一种可能是您复制并粘贴了python文件中的选项卡(可能位于catch行),这些选项卡在您的帖子中变成了空格。在这种情况下,you should change any tabs in your python file to spaces as per PEP-8

当我们这样做的时候,下面的修改版本以出色的成绩通过了lint-checker:

代码语言:javascript
复制
""" Takes a command """

def take_command(speech_rec):
    """
    It takes mircrophone input from the user and return string output
    """

    result = speech_rec.Recognition()
    with speech_rec.mircrophone() as source:
        print("Listening...")
        result.pause_threshold = 1
        audio = result.listen(source)

    try:
        print("Recognizing...")
        query = result.recognize_google(audio, language='eng-ben')
        print(f"User said: {query}\n")

    except (speech_rec.UnknownValueError, speech_rec.RequestError):
        #print(e)
        print("Pardon me? Would be able to repeat that for me?")
        return "None"
    return query

在这段代码中,我们(1)删除了文件末尾的尾随空格,(2)将9个空格改为8个空格,(3)为模块本身和函数添加了文档字符串,(4)将变量名称更改为更有意义的名称和snakecase名称,(5)根据被调用的库捕获了更多特定的异常。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56192487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档