当我不应该为什么我得到一个缩进的块?

问题描述 投票:-1回答:1

它一直在说第一个'之后的预期缩进块,但事实是,这个代码完全正常工作前一个小时,现在由于某种原因它得到了这个错误

任何时候我尝试在该区域附近进行修改它会让我将其更改为python中的函数而不是我想要使用的变量或将其与

def reduceWhitespace(S):

"""
Takes string value and returns with all extra spaces taken out between words
""" 

    hold = S[0]

    for i in range(len(S)-1):
        if S[i] == ' ' and S[i+1] == ' ' :
            continue
        hold += S[i+1]

    return(hold)

它需要做的只是检查一个字符串中的额外空格并吐出结果,而不是所有的空格减一,这样一句话就像一个句子

更新了整个代码

python
1个回答
0
投票

这段代码不会给你错误,因为"""也是缩进的。 """ ... """是一个Python docstring,文档字符串是字符串文字,它出现在类,模块,函数或方法定义中,它作为第一个语句编写,最终将作为实际代码。

请参阅这个有关同一问题的问题:Indent and comments in function in Python

def reduceWhitespace(S):

    """
    Takes string value and returns with all extra spaces taken out between words
    """ 

    hold = S[0]

    for i in range(len(S)-1):
        if S[i] == ' ' and S[i+1] == ' ' :
            continue
        hold += S[i+1]

    return(hold)
© www.soinside.com 2019 - 2024. All rights reserved.