Advertisement
Protocol_

Python Calculator

Oct 27th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. '''
  2. Simple calculator in Python
  3. Coded by Donny
  4. '''
  5. def ans(x):
  6.     print("The answer is : ",x,"\n")
  7. def add(x, y):
  8.     sum = x + y
  9.     ans(sum)
  10. def sub(x, y):
  11.     min = x - y
  12.     ans(min)
  13. def div(x, y):
  14.     divis = x / y
  15.     ans(divis)
  16. def mul(x, y):
  17.     mult = x * y
  18.     ans(mult)
  19.  
  20. usrin = input(">>>  ")
  21.  
  22. divide = usrin.split(' ')
  23. first = divide[0]
  24. op = divide[1]
  25. second = divide[2]
  26.  
  27. if not first:
  28.     print("The first number should not be left blank!")
  29.     exit
  30. elif not second:
  31.     print("The second number should not be left blank!")
  32.     exit
  33. elif not op:
  34.     print("The operator should not be left blank!")
  35.  
  36. first = int(first)
  37. second = int(second)
  38.  
  39. if op == '+':
  40.     add(first, second)
  41. elif op == '-':
  42.     sub(first, second)
  43. elif op == '/':
  44.     div(first, second)
  45. elif op == '*':
  46.     mul(first, second)
  47. else:
  48.     print("ERROR! Invalid OP!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement