smazon09
smazon09

Reputation: 185

I am getting error in my code with try exception

try:
    for v in d.values():
        for item in v[1:]:
            if item != v[0]:
except ValueError:
    raise ValueError('this is inconsistent')

I am getting the following output-

except ValueError:

^ IndentationError: expected an indented block

Upvotes: 1

Views: 65

Answers (2)

heretolearn
heretolearn

Reputation: 6545

The error may be because the program is expecting a statement after if item != v[0]:, but it finds none and is treating the except ValueError: line as the next line which is not at its right Indent.

Upvotes: 1

Makoto
Makoto

Reputation: 106430

If this is your entire code snippet, then after the line if item != v[0]:, there isn't a statement. You would need to put one there.

If you would like to raise an exception instead, then you don't need the try...except block around that piece of code. You would frame it as such:

for v in d.values():
    for item in v[1:]:
        if item != v[0]:
            raise ValueError('this is inconsistent')

Upvotes: 5

Related Questions