Python
  • Getting Started
    • Input and Output
    • Variables
    • Data Types
    • Conditional Structures
      • Humidity Level
      • Financial Transactions
      • Fruit-Vending Machine
      • Magic While
      • FizzBuzz
    • Functions
      • Hashtag Generator
  • Data Structures
    • Lists
    • Dictionary
    • Set
    • Interpreter Expressions
  • OOPs
    • Classes and Objects
    • Exception Handling
      • Simple Calculator
  • System Programming
    • File Operations
    • Directory Navigation
    • Process Creation
    • Threads
Powered by GitBook
On this page
  1. OOPs
  2. Exception Handling

Simple Calculator

I LOVE DOING THIS :)

#!/opt/python3

class Calculator:
	def __init__(self, inputa, inputb):
		self.a = inputa
		self.b = inputb

	def add(self):
		return (self.a + self.b)

	def mul(self):
		return (self.a * self.b)

	def sub(self):
		return (self.a - self.b)

	def div(self):
		if self.b == 0:
			try:
				print("Denominator cannot be zero, Please enter a non-zero number !")
				self.b = int(input())
				return (self.a / self.b)
			
			except ZeroDivisionError as e:
				print(e)
		else:
			return (self.a / self.b)


calc = Calculator(int(input()),int(input()))
print("The Addition of the two numbers are: %d" % calc.add())
print("The Multiplication of the two numbers are: %d" % calc.mul())
print("The Subtraction of the two numbers are: %d" % calc.sub())
print("The Division of the two numbers are: %g" % calc.div())
PreviousException HandlingNextFile Operations

Last updated 2 years ago