Building a Calculator Using Python: A Practical Programming Project 11

Building a Calculator Using Python: A Practical Programming Project



Introduction:

In the world of programming, practical projects are a great way to sharpen your skills and put your knowledge to the test. Recently, I undertook a fascinating project - building a calculator using Python. In this article, I will walk you through the process and showcase the functionality of this calculator.


Project Overview:

The calculator I developed is a command-line application that performs basic arithmetic operations, such as addition, subtraction, multiplication, and division. It offers a user-friendly interface and allows for continuous calculations and multiple operations without restarting the program.


Key Features:


User Interface:

The calculator displays a visually appealing logo upon execution, thanks to the art library.

It prompts the user to enter the first number and then presents a list of available operators.

Arithmetic Operations:

The calculator supports addition, subtraction, multiplication, and division.

By selecting an operator, the user can perform the desired operation on the given numbers.

Continuous Calculation:

After completing an operation, the calculator gives the option to continue using the result for subsequent calculations.

The user can choose to start a new calculation or exit the program.

Technical Implementation:

The project was implemented using the Python programming language and relied on two external libraries, namely replit for clearing the console and art for generating the logo.


The calculator utilizes a dictionary, calculation, to map operators to their respective functions. This allows for easy and dynamic selection of the desired operation. The functions add(), sub(), mul(), and div() were created to perform the arithmetic calculations.


Conclusion:

Building this calculator project has provided me with valuable hands-on experience in Python programming. It allowed me to strengthen my understanding of functions, dictionaries, user input handling, and control flow. Additionally, it demonstrated the importance of modular and organized code structure.

Source code:



from replit import clear
from art import logo
#Functions for different operations
#addition
def add(n1,n2):
  return n1 + n2
#subtraction
def sub(n1,n2):
  return n1 - n2
#multiplication
def mul(n1,n2):
  return n1 * n2
#division
def div(n1,n2):
  return n1 / n2
#Dictionary for operator selection.
calculation = {
    "+":add,
    "-":sub,
    "*":mul,
    "/":div,
  }
#Use recursive function for smooth calculation.
def calculator():
  print(logo)
 
  f_num = float(input("Enter first number: "))
  exitApp = False
  while not exitApp:
    for operator in calculation:
      print(operator)
    sign = input("Pick an operator from the line above\n")
    s_num = float(input("Enter next number: "))
    function = calculation[sign]
    ans1 = function (f_num,s_num)
    print(f"{f_num} {sign} {s_num} = {ans1}")
    restart = input(f"type 'y' to continue calculation with {ans1} or 'n' 
to new calculation or 'exit' to exit\n")
    if restart=='y':
      f_num = ans1
    elif restart == 'n':
      clear ()
      calculator()
    elif restart == 'exit':
      exit()
      exitApp = True
calculator()



 

Next Post Previous Post