Saturday, 28 March 2020

Sum, Subtraction and Multiplication using Class and Object in Python

Sum, Subtraction and Multiplication using Class and Object in Python

I am just learning basic python. I i have committed any mistake please comment there and help me to learn more Thanks

Write code in caluclatorclass.py:


class Calculator:
    enterFirstNumber='Please enter first Number'
    enterSecondNumber='Please enter second Number'
    invalidValueString='Invalid value is passed to'
    def __init__(self):
        print('calculator __init_')
    def sum(self):
        try:
            number1=float(input(self.enterFirstNumber))
            number2=float(input(self.enterSecondNumber))
            print('Sum is:',number1+number2)
        except Exception  as e:
            print('{invalidValueString} sum.'.format(invalidValueString=invalidValueString))
    def subtraction(self):
        try:
            number1=float(input(self.enterFirstNumber))
            number2=float(input(self.enterSecondNumber))
            subtractionValue=number1-number2
            print('Subtraction is:{subtractionValue}'.format(subtractionValue=subtractionValue))
        except Exception  as e:
            print('{invalidValueString} subtraction.'.format(invalidValueString=invalidValueString))
    
    def multiplication(self):
        try:
            number1=float(input(self.enterFirstNumber))
            number2=float(input(self.enterSecondNumber))
            print('Multiplication is:{0}'.format(number1*number2))
        except Exception  as e:
            print('{invalidValueString} multiplication.'.format(invalidValueString=invalidValueString))


Write code in number-examples.py:

from calculatorclass import  Calculator
class Person:
    calculator=None
    def __init__(self,userName):
        #creats Calculator instance here.
        self.calculator=Calculator()
        #capitalize()/title() capitalizes the first Letter of string. 
        print('Welcome {userName}!'.format(userName=userName.capitalize())) 
        
    def whileloop(self):
        self.displayWhatUserCanInput()
        done=False
        while done==False:
            userinput=input('Enter 5 to exit and any other number to continue.')
            if  int(userinput)==5:
                print('Bye!')
                done=True
            else:
                self.displayWhatUserCanInput()
                done=False
    def displayWhatUserCanInput(self):
        print('''Please enter   1 for sum
               2 for subtraction
               3 for multiplication
                ''')
        try:
            options={
                1:self.calculator.sum,
                2:self.calculator.subtraction,
                3:self.calculator.multiplication
            }
            num=input('Please enter 1,2 or 3')
            func=options.get(int(num),'Invalid value found!')
            func()
        except Exception as ex:
            print (ex)
            print('Invalid value found')

class Welcome:
    def __init__(self):
        print('Welcome to the python demo calculator')
        userName=input('Please enter your name:  ')
        if not userName:
            print('You didn\'t enter your name')
        else:
            person=Person(userName)
            person.whileloop()

#Call welcome class here.
welcome=Welcome()

My observation:-

Here i found while creating classes and calling it.

1. If you have __init__() method in class to call it write abc=ClassName
2. If you have __init__(self) method in class  to call it write abc=ClassName()
3. If you have __init__(self,argument) method in class  to call it write abc=ClassName(argument_value) 

to execute it:-

write in terminal:-

py number-examples.py

Sunday, 22 March 2020

Sum, Subtraction and Multiplication Basic Python program


1. Here i used dictionary  mapping to achieve switch statement.
2. Used format method of the string.
3. Print multiple string in single print method by """.
4. Used while loop to iterate over the user input.


def sum():
    try:
        number1=float(input('Please enter first Number'))
        number2=float(input('Please enter second Number'))
        print('Sum is:',number1+number2)
    except Exception  as e:
        print('Invalid value is passed to sum.')
    
def subtraction():
    try:
        number1=float(input('Please enter first Number'))
        number2=float(input('Please enter second Number'))
        subtractionValue=number1-number2
        print('Subtraction is:{subtractionValue}'.format(subtractionValue=subtractionValue))
    except Exception  as e:
        print('Invalid value is passed to sum.')
def multiplication():
    try:
        number1=float(input('Please enter first Number'))
        number2=float(input('Please enter second Number'))
        print('Multiplication is:{0}'.format(number1*number2))
    except Exception  as e:
        print('Invalid value is passed to sum.')
options={
        1:sum,
        2:subtraction,
        3:multiplication
    }
def displayWhatUserCanInput():
    print("""
            Please enter number
            1. For Sum
            2. For Subtraction
            3. For Multiplication
            """)
    try:
        i=int(input("Enter the option 1,2 or 3"))
        func=options.get(i,'Invalid value')
        func()
    except Exception as e:
        print('Invalid entry is passed.')
def whileloop():
    displayWhatUserCanInput()
    done=False
    while done==False:
        userinput=input('Enter 5 to exit and any other number to continue.')
        if  int(userinput)==5:
            print('Bye!')
            done=True
        else:
            displayWhatUserCanInput()
            done=False
whileloop()