Thursday, 19 May 2011

Simple calculator program using python.

hi,

Today i made a simple calculator program in python. The code is given below,..

#! usr/bin/env python
# filename : pycalc.py
"""This is a simple calculator program  """

def addition( val1 , val2 ):
    """This function adds two numbers and returns result"""
    return val1 + val2

def subtraction( val1, val2 ):
    """This function subtracts two numbers and returns result"""
    return val1 - val2

def multiply(val1 , val2):
    """This function multiplies two numbers and returns result"""
    return val1 * val2

def division(val1 , val2):
    """This function divides two numbers and returns result"""
    return val1 / val2


VALUE1 = int(raw_input("enter the first number    "))
VALUE2 = int(raw_input("enter the second number   "))
OPTION = int(raw_input("enter the OPTION \n 1.addition \n \
2.subtraction \n 3.multiplication \n 4.Division\n \n"))

if OPTION == 1:
    print "Result = ",addition(VALUE1 , VALUE2)
elif OPTION == 2:
    print "Result = ",subtraction(VALUE1 , VALUE2)
elif OPTION == 3:
    print "Result = ",multiply(VALUE1 , VALUE2)
elif OPTION == 4:
    print "Result = ",division(VALUE1 , VALUE2)
else:
    print "invalid input, sorry try again"

Snap - Explanation : I used separate  function for each action - then get option and perform particular function using 'if' and 'elif' condition - then print the result.

i/p:

enter the first number    30
enter the second number   10
enter the OPTION
 1.addition
 2.subtraction
 3.multiplication
 4.Division

3

o/p:

Result =  300

No comments:

Post a Comment