-2

I'm starting out in python and just wrote a simple calculator but it seems to have some errors.Pls help me out

a = raw_input("Enter value of a : ")
b = raw_input("Enter value of b : ")

sum = a + b
sub = a - b
mul = a * b
div = a / b

print"1.Addition"
print"2.Subtraction"
print"3.Multiplication"
print"4.Division"


op = raw_input("Enter the operation to be done : ")

if op == 1:
    print"Sum is %d" % sum

elif op == 2:
    print"Difference is %d" % sub

elif op == 3:
    print"Product is %d" % mul

elif op == 4:
    print"Quotient is %d" % div

else:
    print"Invalid operation"    

Error is TypeError : Unsupported operand type for -: 'str' and 'str'

vaultah
  • 44,105
  • 12
  • 114
  • 143
jerink
  • 29
  • 1
  • 4

2 Answers2

-1

You are reading strings, and trying them to subtract them as strings. You have to convert them to numbers first. Just add

a = float(a)
b = float(b)

after the user input

Moreover, sum is a builtin function in python, so you'd better use different names for your variables

blue_note
  • 27,712
  • 9
  • 72
  • 90
-1

change the input into an int by putting int() outside of raw_input.

a = int(raw_input("Enter value: "))

raw_input interprets the user input as strings therefore you need to convert the raw input into an int first before processing them