python - 预期 Python 中的预期 block

标签 python python-3.x

我完全是个新手,我不知道我的代码有什么问题。我尝试多次调整它,但它没有用,并且在我运行代码时一直提示 expected an intended block

def abc(words_list):

number1 = 0
number2 = 0

for L in words_list:
    if L[0] in 'aeiou':
        number1 = number1 + 1

    else:
        number2 = number2 + 1
        first_char = L[0]

        for i in range(1,len[L]):
            L[i-1] = L[i]
        L[-1] = first_char
    L = L + 'ay'

return(number1, number2) 

最佳答案

在函数(def) 开始后,您需要将代码缩进一次。如:

def abc(words_list):


    number1 = 0
    number2 = 0

    for L in words_list:
        if L[0] in 'aeiou':
            number1 = number1 + 1

        else:
            number2 = number2 + 1
            first_char = L[0]

            for i in range(1,len[L]):
                L[i-1] = L[i]
            L[-1] = first_char
        L = L + 'ay'

    return(number1, number2) 

此外,任何空行都需要有正确的缩进。复制粘贴时,例如进出堆栈溢出你可能会丢失空格的缩进,但 python 认为它们也很重要。例如,def 之后的两个空行需要与 number1 开始的行缩进相同。

像 notepad++ 这样的程序可以让你看到空行的缩进情况,任何好的 python IDE 也应该可以工作。

关于python - 预期 Python 中的预期 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15401526/

相关文章:

python - 为什么我的模型不会出现在 Django 的管理页面上?

python - Django 1.7 - 带有自定义应用程序标签的模型发现和应用程序配置

python - pytest fixtures 的范围可以被覆盖吗?

python - 每 1000 的倍数重置 Pandas Cumsum

python - 如何防止 wx 面板与其他 sizer 重叠

python - pytest 不会运行子目录中的测试文件

python - 使用模拟对象而不束缚单元测试

python 字符串作为十六进制以 null 结尾

python - 使用 RegEx 查找并打印土耳其语中的复数单词

python - 根据条件返回列名