How to Create a Calculator Using Python 2.7.x

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
  • Best Python Training in Noida
  • Best Python Training in Delhi
  • Best Python Training in Meerut
  • Best Python Training in India
  • Best Ethical Hacking Training in Noida
  • Best Ethical Hacking Training in Delhi
  • Best Ethical Hacking Training in Meerut
  • Best Ethical Hacking Training in India

Follow ManishHacker1

Comments

Popular posts from this blog

How to create Folder Lock with Password Protected using Python

TOP 10 ANIMATED BATCH FILE PROGRAM

How to Connect Window Machine to Linux Machine Using Python via SSH