Sunday, 31 December 2017

How to Create a Simple Calculator in Python (Try it!)

In this lesson I would teach you how to create a simple calculator in the Python Programming Language. Python 3.6 is used in this tutorial, but the program would also work with other versions of python.

A Brief Explanation
The program contains four functions: Add, Subtract, Multiply and Divide. Each of the function takes two parameters, a and b which represents the two numbers to be evaluated.
The program begins by displaying a menu of four items, with numbers representing each item
1 for Add
2 for Subract
3 for Multiply
4 for Divide

User is prompted to choose from the four operations. The user choice is stored in a variable called operation. Next the user is prompted to enter the first number and the second number.
Based on the user's choice of operation, the appropriate function is called.
The result of the operation is displayed  on the output using the print function.

You can find the code below. Feel free to copy and use

 import sys  
 #****************************************************************************************  
 #PROGRAM BY:  KINDSON THE GENIUS                           *                         
 #SECTION:    PYTHON TUTORIALS                               *  
 #DATE:     1ST JANUARY 2018                                   *  
 #QUESTION No.  YOUR FIRST PROGRAM IN 2018                    *  
 #****************************************************************************************  
   
 print("\n*** PROGRAM TO CREATE SIMPLE MENU-BASED CALCULATOR ******\n\n")  
   
   
 # THE add FUNCTION TO PERFORM ADDITION OF TWO NUMBERS  
 def add(a, b):  
   return a + b  
   
   
 # THE subtract FUNCTION TO PERFORM SUBTRACTION FOR TWO NUMBERS  
 def subtract(a, b):  
   return a - b  
   
   
 # THE multiply FUNCTION TO PERFORM MULTIPLICATION OF TWO NUMBERS  
 def multiply(a, b):  
   return a * b  
   
 # THE DIVIDE FUNCTION TO PEFORM DIVISION FOR TWO NUMBERS  
 def divide(a, b):  
   return a / b  
   
 #DISPLAY THE MENU  
 print("CHOOSE AN OPERATION. ")  
 print("1. ADD")  
 print("2. SUBTRACT")  
 print("3. MULTIPLY")  
 print("4. DIVIDE")  
 print("5. EXIT")  
   
 #GET USER INPUT  
 operator = input("Select an operation (1,2,3,4,5): ")  
   
 #EXIT THE PROGRAM IF THE INPUT IS 5  
 if operator == '5':  
   sys.exit()  
   
 number1 = int(input("Enter first number:"))  
 number2 = int(input("Enter second number:"))  
   
 #CONVERT THE INPUTS TO INTEGER  
 number1 = int(number1)  
 number2 = int(number2)  
   
 if operator == '1':  
   result = add(number1,number2)  
   print(number1,"+",number2,"=", result)  
   
 elif operator == '2':  
   result = subtract(number1,number2)  
   print(number1,"-",number2,"=", result)  
   
 elif operator == '3':  
   result = multiply(number1,number2)  
   print(number1,"*",number2,"=", result)  
   
 elif operator == '4':  
   result = add(number1,number2)  
   print(number1,"/",number2,"=", result)  
     
 else:  
   print("Invalid input")  
   
   
   
How to Run 
  • Open Python Idle IDE
  • Click on File> New File
  • Copy and paste the program
  • Save the file with a name
  • Click on Run > Run Module