-1

It keeps saying expected an indented block right after the first ' ' but the thing is, an hour before this code worked perfectly fine and now it's getting this error for some reason

any time I try to modify near that area it keeps making me change it to a function within python instead of the variable I want to use or compare it to

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)

All it needs to do is check for extra spaces in a string and spit out the result without all the spaces minus one so that a sentence looks like a sentence

Updated for entire code

mustaccio
  • 18,234
  • 16
  • 48
  • 57
  • 4
    `return` needs to be within a function `def`, or are you just not showing that? – 101 Apr 12 '19 at 02:24
  • this is all inside a defined function, i'm just not showing that part, sorry. It's weird because I touched nothing and ran it one more time an hour later to make sure it was still okay and it suddenly showed this error – user11349209 Apr 12 '19 at 02:26
  • @user11349209 that's not a thing. You've probably inserted a tab somewhere. Post your actual function and the actual stack trace of the error. – Adam Smith Apr 12 '19 at 02:26
  • Updated - the def of the function didn't get grayed out but it's the first line, with the ''' xxx ''' comment line after that. Is it because my comment isn't indented? – user11349209 Apr 12 '19 at 02:34
  • 4
    It looks like your docstring is not properly indented. Remember that the docstring is code just like any other expression, and must be indented along with the rest of the function body. – larsks Apr 12 '19 at 02:37
  • 1
    It's dumb but makes sense. Thanks! – user11349209 Apr 12 '19 at 02:42

1 Answers1

0

This code won't give you error because the """ is indented as well. """ ... """ is a Python docstring, the documentation string which is string literal, and it occurs in the class, module, function or method definition, and it is written as a first statement and it will end up as an actual code.

Refer to this SO question about same question : 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)
gameon67
  • 3,981
  • 5
  • 35
  • 61