Magomed
Magomed

Reputation: 13

Why can't I add for loop within a block without occuring indentation error?

def PhoneNumber(text):
    if len(text) != 14:
        return False
    for i in range(0, 4):
        if not text[i].isdecimal():
            return False
    if text[4] != '-':
        return False
    for i in range(5, 8):
        if not text[i].isdecimal():
            return False
    if text[8] != '-':
         return False
    for i in range(9, 11):
        if not text[i].isdecimal():
            return False
    if text[11] != '-':
        
 ** for i in range(12, 14):
        if not text[i].isdecimal():
            return False **
    return True

    

Upvotes: 0

Views: 62

Answers (1)

thesilican
thesilican

Reputation: 763

Just before the for loop you have the following:

    # This if block is empty
    if text[11] != '-':
        
    for i in range(12, 14):
        if not text[i].isdecimal():
            return False
    return True

In python, empty code blocks have to use the pass keyword

So, just add an indented pass to the empty if statement

    # That's better :)
    if text[11] != '-':
        pass
    for i in range(12, 14):
        if not text[i].isdecimal():
            return False
    return True

Upvotes: 2

Related Questions