15,903,697 members
1.00/5 (1 vote)
See more:
I tired implementing a history list for developing a calculator in Python earlier. I can't overcome the indentation error it displays right here."
Python
```past_calc.append(last_calculation)
else:
print("Unrecognized operation")     ```
"

What I have tried:

Python
```past_calc=[]
return a+b

def subtract(a,b):
return a-b

def multiply (a,b):
return a*b

def divide(a,b):
try:
return a/b
except Exception as e:
print(e)
def power(a,b):
return a**b

def remainder(a,b):
return a%b

def history():
if len(past_calc)==0:
print("No past calculations to show")
else:
for x in range(0,len(past_calc)):
print(past_calc[x])

def select_op(choice):
if (choice == '#'):
return -1
elif (choice == '\$'):
return 0
elif(choice=='?'):
history()
elif (choice in ('+','-','*','/','^','%')):
while (True):
num1s = str(input("Enter first number: "))
print(num1s)
if num1s.endswith('\$'):
return 0
if num1s.endswith('#'):
return -1

try:
num1 = float(num1s)
break
except:
print("Not a valid number,please enter again")
continue

while (True):
num2s = str(input("Enter second number: "))
print(num2s)
if num2s.endswith('\$'):
return 0
if num2s.endswith('#'):
return -1
try:
num2 = float(num2s)
break
except:
print("Not a valid number,please enter again")
continue

result = 0.0
last_calculation = ""
if choice == '+':
elif choice == '-':
result = subtract(num1, num2)
elif choice == '*':
result = multiply(num1, num2)
elif choice == '/':
result =  divide(num1, num2)
elif choice == '^':
result = power(num1, num2)
elif choice == '%':
result = remainder(num1, num2)
else:
print("Something Went Wrong")

last_calculation =  "{0} {1} {2} = {3}".format(num1, choice, num2, result)
print(last_calculation )

past_calc.append(last_calculation)
else:
print("Unrecognized operation")

while True:
print("Select operation.")
print("2.Subtract : - ")
print("3.Multiply : * ")
print("4.Divide   : / ")
print("5.Power    : ^ ")
print("6.Remainder: % ")
print("7.Terminate: # ")
print("8.Reset    : \$ ")
print("8.History  : ? ")

# take input from the user
choice = input("Enter choice(+,-,*,/,^,%,#,\$,?): ")
print(choice)
if(select_op(choice) == -1):
#program ends here
print("Done. Terminating")
exit()```
Posted
Updated 14-Sep-23 8:28am
v2

## Solution 1

Indentation in Python is significant: it controls what is and isn't in a block of code.
All the contiguous code indented by the same amount (or more) is part of teh same code block:
Python
```while foo == bar:
Part of loop
if (foo + bar == foobar:
Still part of loop
Not part of loop -0 executed once the loop exits.```

So look at your code carefully:
Python
```past_calc.append(last_calculation)
else:
print("Unrecognized operation")```
The first line "pulls the block" right back to the beginning of the line, so there is no `if` for the `else` to match up with.

F Atheeya 19-Aug-23 11:30am
how can i fix it as for this code ?
OriginalGriff 19-Aug-23 11:55am
Only you can answer that - I have no idea what that `else` is supposed to be connected to! You wrote the code, you understand it - and only you have any idea what you intended when you wrote it! :laugh:
F Atheeya 19-Aug-23 13:12pm
haha thnx for the solution ..i got it right
OriginalGriff 19-Aug-23 13:39pm
You're welcome!

## Solution 2

You have the following and the last three lines have the wrong indentation:
Python
```    last_calculation =  "{0} {1} {2} = {3}".format(num1, choice, num2, result)
print(last_calculation )

past_calc.append(last_calculation)
else:
print("Unrecognized operation")```

They need to be changed to:
Python
```....last_calculation =  "{0} {1} {2} = {3}".format(num1, choice, num2, result)
....print(last_calculation )
....past_calc.append(last_calculation)
..else:
....print("Unrecognized operation")```

NOTE: I have used dots to represent spaces, as the formatter is destroying my indentations.

v4