IndentationError: expected an indented block
Question:
Can someone please tell me what I’m doing wrong in this Python code sample?
for i in range(len(Adapters)):
print Adapters[i]
I’m trying to list/print out the Array content but the code gives me error on print command: IndentationError: expected an indented block
Answers:
You need to indent inside the for loop
block
for i in range(len(Adapters)):
print(Adapters[i])
A better way would be:
for item in Adapters:
print(item)
You need to indent the print
statement inside the body of the for-loop
for i in range(len(Adapters)):
print Adapters[i]
If you want to streamline your code, the 2nd loop suggested by @jamylak is the way to go.
When you have a block that starts with a phrase that ends in a colon, you need to indent the next lines until you are done. This goes for loops, if statements, etc.
if 0!=-1:
print "Good!"
while 0!=-1:
print "BWAHAHAHA"
for i in range(1,100):
print i
try:
print blah
except NameError:
print "Blah is not defined"
As your error says you are missing an indentation on the second line. Unlike other languages like Java, Python uses indentation to determine the grouping of the statements.
It should be:
for i in range(len(Adapters)):
print Adapters[i]
Can someone please tell me what I’m doing wrong in this Python code sample?
for i in range(len(Adapters)):
print Adapters[i]
I’m trying to list/print out the Array content but the code gives me error on print command: IndentationError: expected an indented block
You need to indent inside the for loop
block
for i in range(len(Adapters)):
print(Adapters[i])
A better way would be:
for item in Adapters:
print(item)
You need to indent the print
statement inside the body of the for-loop
for i in range(len(Adapters)):
print Adapters[i]
If you want to streamline your code, the 2nd loop suggested by @jamylak is the way to go.
When you have a block that starts with a phrase that ends in a colon, you need to indent the next lines until you are done. This goes for loops, if statements, etc.
if 0!=-1:
print "Good!"
while 0!=-1:
print "BWAHAHAHA"
for i in range(1,100):
print i
try:
print blah
except NameError:
print "Blah is not defined"
As your error says you are missing an indentation on the second line. Unlike other languages like Java, Python uses indentation to determine the grouping of the statements.
It should be:
for i in range(len(Adapters)):
print Adapters[i]