Your younger brother is studying in school. His computer teacher gives homework to make a calculator for six operations: addition, subtraction, multiplication, division, power, and modulo on two integers. As you are about to become an engineer, so he expected help from your side to develop the program. Therefore, write Calc.py module that define a separate function for implementing all the above-mentioned operations. Then import Calc.py in your RollNo_W12A_1.py file. In RollNo_W12A_1.py, define a function Arithmatic(a, b, op) which calls the respected function defined in Calc.py to perform the required operation. Also handle the possible exceptions and display the
Example-1
Example-2
Example-3
Example-4
Input:
5
6
+
Output:
11
Input:
15
9
*
Output:
135Input:
15
9
&
Output:
Invalid operation
Input:
2
4
^
Output:
16
Store the following function in the Calc.py module:
def Arithmatic(x, y, op):
"""Calculator for six operations"""
result = 0
while True:
if op == '+':
result = x+y
print(x, "+", y, "=", result)
break
if op == '-':
result = x-y
print(x, "-", y, "=", result)
break
if op == '*':
result = x*y
print(x, "*", y, "=", result)
break
if op == '/':
result = x/y
print(x, "/", y, "=", round(result, 2))
break
if op == '%':
result = x % y
print(x, "%", y, "=", result)
break
if op == '^':
result = x ^ y
print(x, "^", y, "=", result)
break
if op not in operations:
print("Invalid operator")
break
Import Calc.py module in the RollNo_W12A_1.py file using the following code:
from Calc import Arithmatic
x = int(input())
y = int(input())
op = input()
operations = ['+', '-', '^', '*', '/', '%']
Arithmatic(x, y, op)
Comments
Leave a comment