How to Create a Calculator Using Python 2.7.x

Python Tutorial
Python Secret #Author: ManishHacker1

Hello guys, I am back. Today we will create a simple Calculator using Python 2.7.x version. This calculator perform basic mathematical operation such as add, minus, multiply and divide two number entered by user.

Source Code


#ManishHacker1
#https://www.facebook.com/ManishHacker1
#https://www.facebook.com/krypsec
from __future__ import division
import os
import time

# This function adds two numbers 
def add(x, y):
   return x + y

# This function subtracts two numbers 
def subtract(x, y):
   return x - y

# This function multiplies two numbers
def multiply(x, y):
   return x * y

# This function divides two numbers
def divide(x, y):
   return x / y
while True:
    print("Select operation.")
    print("1.Add")
    print("2.Subtract")
    print("3.Multiply")
    print("4.Divide")

# Take input from the user
    try:
        choice = int(raw_input("Enter choice(1/2/3/4):"))
    except:
        print "Invalid Value, Please Enter Right Option"
        time.sleep(5)
        os.system('cls')
    else:
        try:
            num1 = int(raw_input("Enter first number: "))
            num2 = int(raw_input("Enter second number: "))
        except:
            print "Invalid Value"
            time.sleep(5)
            os.system('cls')
        
        else:
            if choice == 1:
                print num1,'+',num2,'=', add(num1,num2)
                time.sleep(5)
                os.system('cls')
            elif choice == 2:
                print num1,"-",num2,"=", subtract(num1,num2)
                time.sleep(5)
                os.system('cls')
            elif choice == 3:
                print num1,"*",num2,"=", multiply(num1,num2)
                time.sleep(5)
                os.system('cls')
            elif choice == 4:
                try:
                    print num1,"/",num2,"=", divide(num1,num2)
                    time.sleep(5)
                    os.system('cls')
                except ArithmeticError:
                    print "Arithmetic Error, Please Enter Right Value"
                    time.sleep(3)
                    os.system('cls')
            else:
                 print "Invalid input"
                 time.sleep(5)
                 os.system('cls')

Thank You for reading this article. And also like my FB page givin below link and share it.

Kryptora Digital Security Provided Python Training and Ethical Hacking Training

Source: Python Secret

Source: Python

Follow ManishHacker1

Summary
How to Create a Calculator Using Python 2.7.x
Article Name
How to Create a Calculator Using Python 2.7.x
Description
This calculator perform basic mathematical operation such as add, minus, multiply and divide two number entered by user.
Author
Publisher Name
Kryptora
Publisher Logo

Leave a Reply

avatar
  Subscribe  
Notify of

Pin It on Pinterest

Optimization WordPress Plugins & Solutions by W3 EDGE