IndentationError expected an indented block

0 votes
class Solution(object):
   def calculate(self, s):
      """
      :type s: str
      :rtype: int
      """
      stack = []
      i = 0
      x=""
      for j in s:
      if j !=" ":
      x+=j
      s = x
      n = len(s)
      while i<n:
         if s[i] == '/':
            i+=1
         num,i = self.make_num(s,i)
         if stack[-1]<0:
            stack[-1] = -1*(abs(stack[-1])/num)
         else:
            stack[-1] = stack[-1]/num
      elif s[i] == '*':
         i+=1
         num,i = self.make_num(s,i)
         stack[-1] = stack[-1]*num
      elif s[i] == '-':
         i+=1
         num,i = self.make_num(s,i)
         stack.append(-num)
      elif s[i] =='+':
         i+=1
         num,i = self.make_num(s,i)
         stack.append(num)
      else:
         num,i = self.make_num(s,i)
         stack.append(num)
         i+=1
         return sum(stack)
   def make_num(self,s,i):
      start = i
      while i<len(s) and s[i]!= '/' and s[i]!= '*' and s[i]!= '-' and s[i]!='+':
      i+=1
   return int(s[start:i]),i-1
Aug 8, 2020 in Python by nishit
• 120 points
3,987 views

1 answer to this question.

0 votes

Hello @ nishit,

Indentation means the space from margin to the begin of characters in a line. In most popular programming languages, spaces or indentation are just used to make the code look better and be easier to read. In Python , it is actually part of this programming language. Because the Python language is a very sensitive language for indentation, it has caused confusion for many beginners. Putting in an extra space or leaving one out where it is needed will surely generate an error message . Some common causes of this error include:

  1. Forgetting to indent the statements within a compound statement
  2. Forgetting to indent the statements of a user-defined function.

The error message IndentationError: expected an indented block would seem to indicate that you have an indentation error. It is probably caused by a mix of tabs and spaces.

Here is the code which remove your IndentationError: expected an indented block:

class Solution(object):
   def calculate(self, s):
      """
      :type s: str
      :rtype: int
      """
      stack = []
      i = 0
      x=""
      for j in s:
        if j !=" ":
          x+=j
          s = x
          n = len(s)
          while i<n:
           if s[i] == '/':
             i+=1
             num,i = self.make_num(s,i)
           if stack[-1]<0:
             stack[-1] = -1*(abs(stack[-1])/num)
           else:
             stack[-1] = stack[-1]/num
        elif s[i] == '*':
          i+=1
          num,i = self.make_num(s,i)
          stack[-1] = stack[-1]*num
        elif s[i] == '-':
          i+=1
          num,i = self.make_num(s,i)
          stack.append(-num)
        elif s[i] =='+':
          i+=1
          num,i = self.make_num(s,i)
          stack.append(num)
        else:
          num,i = self.make_num(s,i)
          stack.append(num)
          i+=1
          return sum(stack)
   def make_num(self,s,i):
      start = i
      while i<len(s) and s[i]!= '/' and s[i]!= '*' and s[i]!= '-' and s[i]!='+':
          i+=1
      return int(s[start:i]),i-1

Hope it resolve your error!!

Thank you!

answered Aug 10, 2020 by Niroj
• 82,680 points

Related Questions In Python

+1 vote
3 answers

Python error "IndentationError: expected an indented block"

Python requires its code to be indented ...READ MORE

answered Jun 17, 2019 in Python by Varun

edited Jun 17, 2019 221,700 views
0 votes
1 answer

I was written the python program, am getting error message "IndentationError: expected an indented block"

Hi@Thiyagarajan, You have one else block in your ...READ MORE

answered May 27, 2020 in Python by MD
• 95,340 points
2,566 views
0 votes
1 answer

IndentationError: expected an indented block

Hi, @Baker, Putting in extra space or leaving ...READ MORE

answered Oct 19, 2020 in Python by Gitika
• 65,850 points
375 views
0 votes
0 answers

IndentationError: expected an indented block in phyton

Hi I i keep getting that error ...READ MORE

Sep 30, 2021 in Python by anonymous
• 120 points
22 views
0 votes
1 answer

I got expected an indented block error.

Hi, You used one function in your code, ...READ MORE

answered Jun 16, 2020 in Python by MD
• 95,340 points
1,483 views
0 votes
1 answer

I've been trying to run this code, but the error says "Expected an indented block" for the line, " word_as_list[index] = guess."

Hi, @Paradox, The error message IndentationError: expected an indented ...READ MORE

answered Nov 21, 2020 in Python by anonymous
• 65,850 points
341 views
0 votes
1 answer

How can I raise an exception in Python so that it can later be caught via an except block?

It's pretty simple to raise a query  raise ...READ MORE

answered May 29, 2019 in Python by Umesh
292 views
+2 votes
3 answers

How can I play an audio file in the background using Python?

down voteacceptedFor windows: you could use  winsound.SND_ASYNC to play them ...READ MORE

answered Apr 4, 2018 in Python by charlie_brown
• 7,710 points
10,628 views
0 votes
1 answer

Error: current transaction is aborted, commands ignored until end of transaction block?

Hii kartik, To get rid of the error, roll ...READ MORE

answered Apr 23, 2020 in Python by Niroj
• 82,680 points
6,854 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP