# Python Code Error in Calculator Program

Hello this is Gulshan Negi
Well, I am writing a Python program for making calculator, but it shows some errors at the time of execution. I don’t know what I am missing here.

import os

os.system(‘cls’ if os.name == ‘nt’ else ‘clear’)

continue_calc = ‘y’

num_1 = float(input('Enter a number: '))
num_2 = float(input('Enter another number: ‘))
ans = num_1 + num_2
values_entered = 2
print(f’Current result: {ans}’)

while continue_calc.lower() == ‘y’:
continue_calc = (input('Enter more (y/n): '))
while continue_calc.lower() not in [‘y’, ‘n’]:
continue_calc = (input('Enter more (y/n): '))

``````   if continue_calc.lower() == 'n':
break
num = float(input('Enter another number: '))
ans += num
print(f'Current result: {ans}')
values_entered += 1
``````

return [ans, values_entered]

def subtraction():
os.system(‘cls’ if os.name == ‘nt’ else ‘clear’)
print(‘Subtraction’)

continue_calc = ‘y’

num_1 = float(input('Enter a number: '))
num_2 = float(input('Enter another number: ‘))
ans = num_1 - num_2
values_entered = 2
print(f’Current result: {ans}’)

while continue_calc.lower() == ‘y’:
continue_calc = (input('Enter more (y/n): '))
while continue_calc.lower() not in [‘y’, ‘n’]:
continue_calc = (input('Enter more (y/n): '))

``````   if continue_calc.lower() == 'n':
break
num = float(input('Enter another number: '))
ans -= num
print(f'Current result: {ans}')
values_entered += 1
``````

return [ans, values_entered]

def multiplication():
os.system(‘cls’ if os.name == ‘nt’ else ‘clear’)
print(‘Multiplication’)

continue_calc = ‘y’

num_1 = float(input('Enter a number: '))
num_2 = float(input('Enter another number: ‘))
ans = num_1 * num_2
values_entered = 2
print(f’Current result: {ans}’)

while continue_calc.lower() == ‘y’:
continue_calc = (input('Enter more (y/n): '))
while continue_calc.lower() not in [‘y’, ‘n’]:
continue_calc = (input('Enter more (y/n): '))

``````   if continue_calc.lower() == 'n':
break
num = float(input('Enter another number: '))
ans *= num
print(f'Current result: {ans}')
values_entered += 1:
``````

return [ans, values_entered]

def division():
os.system(‘cls’ if os.name == ‘nt’ else ‘clear’)
print(‘Division’)

continue_calc = ‘y’

num_1 = float(input('Enter a number: '))
num_2 = float(input('Enter another number: '))
while num_2 == 0.0:
print(‘Please enter a second number > 0’)
num_2 = float(input('Enter another number: '))

ans = num_1 / num_2
values_entered = 2
print(f’Current result: {ans}')

while continue_calc.lower() == ‘y’:
continue_calc = (input('Enter more (y/n): '))
while continue_calc.lower() not in [‘y’, ‘n’]:
continue_calc = (input('Enter more (y/n): '))

``````   if continue_calc.lower() == 'n':
break
num = float(input('Enter another number: '))
while num == 0.0:
print('Please enter a number > 0')
num = float(input('Enter another number: '))
ans /= num
print(f'Current result: {ans}')
values_entered += 1
``````

return [ans, values_entered]

def calculator():
quit = False
while not quit:
results = ()
print(‘Simple Calculator in Python!’)
print(‘Enter 's' for substraction’)
print(‘Enter 'm' for multiplication’)
print(‘Enter 'd' for division’)
print(‘Enter 'q' to quit’)

``````   choice = input('Selection: ')

if choice == 'q':
quit = True
continue

if choice == 'a':
print('Ans = ', results, ' total inputs: ', results)
elif choice == 's':
results = subtraction()
print('Ans = ', results, ' total inputs: ', results)
elif choice == 'm':
results = multiplication()
print('Ans = ', results, ' total inputs: ', results)
elif choice == 'd':
results = division()
print('Ans = ', results, ' total inputs: ', results)
else:
print('Sorry, invalid character')
``````

if name == ‘main’:
calculator()

I also check and take reference from here, but I don’t know what I am missing in my code. Can anyone give their suggestions on this.
Thanks