Python program to Build a Simple Calculator

print("Zero operation terminates program!")
while True:
 o = input("Choose Operator(+, -, *, /): ")
 if o == "0":
   break
 if o in ('+','-','*','/'):
  x = float(input("Enter the value of x = "))
  y = float(input("Enter the value of y = "))
  if o == '+':
   print("%.2f" % (x+y))
  elif o == '-':
   print("%.2f" % (x-y))
  elif o == '*':
   print("%.2f" % (x*y))
  elif o == '/':
    if y != 0:
     print("%.2f" % (x / y))
    else:
     print("Error! Division by zero...")
 else:
  print("Invalid operator")

 

 

Output:

Zero operation terminates program!
Choose Operator(+, -, *, /): +
Enter the value of x = 12
Enter the value of y = 27
39.00

Enter the value of x = 12
Enter the value of y = 4
3.00
Choose Operator(+, -, *, /):

Subscribe For Daily Updates

100+ Python Pattern Examplespython pattern examples - star patterns, number patterns