How To Programming

How to Make a Calculator with Python

How to make a calculator with python
Written by Robin Okwanma

Building a calculator program with python is usually one of the simplest projects a learner may attempt. You can make a simple calculator using if-else statements or functions or both. In this article, we’ll be discussing how to make a calculator program with python.

Requirements

For this to be successful, you need to understand basic python syntax, you need python installed on your PC too. Finally, you need to understand the logic of the calculator program.

Read Also- Web Scraping With Beautiful Soup and Python

Understanding the Logic

So you want to build a basic calculator with python, my personal approach i think most people seem to forget is “understanding the logic”. If you can understand what exactly it is you are doing and how it should work, you will be able to execute the idea in your head.

How should the calculator program we’re about to build work? Just like a normal calculator, but way more simple. Our program should be able to carry out basic mathematical operations like addition, subtraction, division and multiplication.

You can get make a calculator in python using functions or if else statements.

Make a Calculator with Python Using If-else statements

Logic– Think of it like this, there are 4 operations; addition, subtractions, multiplication and division, first you list out the operations and let the user choose one. Once an operation is chosen, the program then asks for the first number then the second number. The output will be first number followed by the operation then second number. The logic might sound rough but you should get the point. Check the code below;

#Using Simple if else statements

choices = (
   '1- addition\n'
   '2- multiplication\n'
   '3- subtraction\n'
   '4- division\n'
)

print('These are the available operations you can make, please choose correctly\n', choices)

operator input = int(input('Choose an operation\n 1/2/3/4\n'))

if operatorinput == 1:
   print("You are adding")
   number1 = float(input("number1 \n"))
   number2 = float(input("number2 \n"))
   print(number1, "+", number2, "=", number1 + number2)
elif operatorinput == 2:
   print("You are multiplying")
   number1 = float(input("number1 \n"))
   number2 = float(input("number2 \n"))
   print(number1, "*", number2, "=", number1 * number2)
elif operatorinput == 3:
   print("You are subtracting")
   number1 = float(input("number1 \n"))
   number2 = float(input("number2 \n"))
   print(number1, "-", number2, "=", number1 - number2)
elif operatorinput == 4:
   print("You are dividing")
   number1 = float(input("number1 \n"))
   number2 = float(input("number2 \n"))
   print(number1, "/", number2, "=", number1 / number2)
else:
 print('Error, Select a valid number between 1(addition), 2(multiplication), 3(subtraction), and 4(division)')

Read Also- Django vs Flask: Choosing the Right Framework

Make a Calculator with Python Using Functions

Logic– Unlike the if else method, using function is slightly different. You begin by defining a function for each operation then calling the function after operands are given. Functions are a good way of making code less redundant.

#Using Functions

def addition(x, y):
   return x+y

def multiplication(x, y):
   return x*y

def subtraction(x, y):
   return x-y

def division(x, y):
   return x/y

choices = (
   '1- addition\n'
   '2- multiplication\n'
   '3- subtraction\n'
   '4- division\n'
)

print('These are the available operations you can make, please choose correctly\n', choices)

While True:
   operator input = int(input('Choose an operation\n 1/2/3/4\n'))
   number1 = float(input('number1 \n'))
   number2 = float(input('number2 \n'))
   if operatorinput == 1:
      print(number1, '+', number2, '=', addition(number1, number2))
   elif operatorinput == 2:
      print(number1, '*', number2, '=', multiplication(number1, number2))
   elif operatorinput == 3:
      print(number1, '-', number2, '=', subtraction(number1, number2))
   elif operatorinput == 4:
      print(number1, '/', number2, '=', division(number1, number2))

   another = input("Let's run another calculation? (yes/no)")
   if another == "no":
        break
   else:
     print('Error, Select a valid number between 1(addition), 2(multiplication), 3(subtraction), and 4(division)')

Read Also- 10 Things a Smartwatch Can Do Without A Phone

There you have it! Subscribe to our newsletter and stay updated. Next, we’ll show you how to use tkinter to make a calculator application.

About the author

Robin Okwanma

Leave a Comment