BOP Blog: Simple Python Calculator

BytesOfProgress

Simple Python Calculator

16th December 2022 / 11:00 PM

I wrote a simple Terminal-Based Calculator in Python, which can be used for Addition, Subtraction, Muliplication and Division of 2 Numbers. Also, when the user tries to divide by zero, the program will tell him or her that it's not possible.


This is what the code looks like:

# Loop Start
  while True:

  # Global Variables

  # Addition, Subtraction, Multiplication or Division?
    calculation = input("Which Type of Calculation do you want to use?\nAddition ( 1 )\nSubtraction ( 2 )\nMultiplication ( 3 )\nDivision ( 4 )\n")

  # User gives number 1, number 2.
    a = float(input('Enter Number 1: '))
    b = float(input('Enter Number 2: '))

  # Result
    addition = (a+b)
    subtraction = (a-b)
    multiplication = (a*b)
    division = (a/b)

  #--------------------------------------------------------------------------------------------------------------------------------------------

  # Addition

    if (calculation) == "1":
        print (('Your Result:'), addition)

  # Subtraction

    if (calculation) == "2":
        print (('Your Result:'), subtraction)

  # Multiplication

    if (calculation) == "3":
        print (('Your Result:'), multiplication)

  # Division

    if calculation == "4":
        try:
            division = a / b
            print("Your Result:", division)
        except ZeroDivisionError:
            print("Numbers can not be zero!")

  #---------------------------------------------------------------------------------------------------------------------------------------------

  # Restart?

    restart = input("Would you like to do another Calculation? ( y / n )\n")

    if (restart) == ("y"):
        continue
    else:
        break



back