0

Can someone tell me why i recieve this error IndentationError: expected an indented block

Here is my code

def resi(k,visina):
visina.sort()
odg=float("inf") 
s = 0 
for i, x in enumerate(visina):
s += x 
if i >= k:
s -= visina[i - k] 
if i >= k - 1: 
odg= min(odg, x * k - s) 
return odg
visina = input("Enter first number:-")
k = input("Enter second number:-")
resenje=resi(k,visina)
print(resenje)
ilic25
  • 13
  • 2

3 Answers3

2

Python relies on indentation to know the structure of the code. I highly recommend going through a python tutorial to learn more about how to indent your code, as it is one of the most fundamental things to know about python.

I have included one way of indenting your code here. I don't know if this is the behavior you want, though.

def resi(k,visina):
    visina.sort()
    odg=float("inf") 
    s = 0 
    for i, x in enumerate(visina):
        s += x 
        if i >= k:
            s -= visina[i - k] 
        if i >= k - 1: 
            odg= min(odg, x * k - s) 
    return odg

visina = input("Enter first number:-")
k = input("Enter second number:-")
resenje=resi(k,visina)
print(resenje)
jkr
  • 17,119
  • 2
  • 42
  • 68
1

In python, indentation is very important. It helps to find which statement belongs to which code block. Below code solves your indentation error. But your code may not work as you expected as you are taking the second input as a string and trying to sort it using the sort() method which is not a string method.

def resi(k,visina):
    visina.sort()
    odg=float("inf") 
    s = 0 
    for i, x in enumerate(visina):
        s += x 
        if i >= k:
            s -= visina[i - k] 
        if i >= k - 1: 
            odg= min(odg, x * k - s) 
    return odg
visina = input("Enter first number:-")
k = input("Enter second number:-")
resenje=resi(k,visina)
print(resenje)
1

An indentation error occur when you don't put enough space or enter more than enough space in your code.try this-- after every ':' type the code for the next line after 2 spaces. for example,

def worder(word):
  for i in len(word):
    if i==4:
      print(word)
CODER
  • 11
  • 2
  • Constructive feedback: You could further explain what you mean by "enter more than enough space": Does the number of white spaces matter if the indented block has only one line? – mcsoini Apr 17 '21 at 18:01