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 7 in Python 235 views

## 1 answer to this question.

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

Thank you! answered Aug 9 by
• 58,500 points

## Python error "IndentationError: expected an indented block"

Putting in an extra space or leaving ...READ MORE

## 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

## 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

## What is an array in Python? How to declare it?

Python doesn't have a native array data ...READ MORE

+1 vote

## Count the frequency of an item in a python list

To count the number of appearances: from collections ...READ MORE

## How can I find out the index of an element from row and column in Python?

You probably want to use np.ravel_multi_index: [code] import numpy ...READ MORE