Python Forum
Need help with "IndentationError: expected an indented block"
Thread Rating:
  • 3 Vote(s) - 3.33 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with "IndentationError: expected an indented block"
#1
Hello!
Since python has a min(array) command I tried to make one manually. However I ran into a problem. Here is my code (file name is "listan.minimi.py"):

import sys


arr = [1,2,3]
def print2Smallest(arr):

arr_size = len(arr)
if arr_size < 2:
    print "invalid output"
    return

first = second = sys.maxint #antaa muuttujille first ja second suurimman mahdollisen lista-arvon pythonissa
for i in range(0, arr_size)

    if arr(i) < first:
    second = first
    first = arr[i]

    elif(arr[i]<second and arr[i]=!first):
        second = arr[i];

if (second == sys.maxint):
    print "No second smallest element"
else:
    print 'The smallest element is', first 'and'\
		' second smallest element is', second
Here is what I get out:

File "listan.minimi.py", line 7
arr_size = len(arr)
^
IndentationError: expected an indented block

I tried replacing tabs with spaces but still I don't seem to understand the problem here. Thanks!
Reply
#2
lines 7 and 8 need to be indented
and if 12 - 26 are part of function it will have to be indented as well
Reply
#3
You need to indent lines under a definition just like how you would indent lines under an "if" or an else" statement for example.

Lines that are under the indent are part of the definition. So you need to move all those lines below def by one tab.
Reply
#4
There is no indent space between the function declaration and body.


It should be like this.
def print2Smallest(arr):
 
	arr_size = len(arr)
	if arr_size < 2:
		print "invalid output"
		return
Text editors like Notepad++ have the option of indenting the lines of code.
Select the text within the function body and use indent option.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  IndentationError: expected an indented block python1980 3 7,732 Dec-03-2017, 10:00 PM
Last Post: buran
  IndentationError: unexpected indent belikewater 6 10,953 Oct-31-2017, 12:32 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020