Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Expected an indented block [duplicate]

I thought everything was properly indented here but I am getting an IndentationError: expected an indented block at the else: statement. Am I making an obvious mistake here?

def anti_vowel(text):
    new_string = ""
    vowels = "aeiou"
    for letter in text:
       for vowel in vowels:
           if (lower(letter) == vowel):
               #do nothing
           else:
               #append letter to the new string
               new_string += letter
    return new_string
like image 787
ricoflow Avatar asked Jan 23 '26 22:01

ricoflow


2 Answers

Do nothing translates to using the pass keyword to fill an otherwise empty block (which is not allowed). See the official documentation for more information.

def anti_vowel(text):
    new_string = ""
    vowels = "aeiou"
    for letter in text:
       for vowel in vowels:
           if (lower(letter) == vowel):
               #do nothing
               pass
           else:
               #append letter to the new string
               new_string += letter
    return new_string
like image 102
Cilyan Avatar answered Jan 25 '26 12:01

Cilyan


You need to put something inside the if block. If you don't want to do anything, put pass.

Alternatively, just reverse your condition so you only have one block:

if lower(letter) != vowel:
    new_string += letter

Incidentally, I don't think your code will do what you intend it to do, but that's an issue for another question.

like image 28
BrenBarn Avatar answered Jan 25 '26 13:01

BrenBarn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!