Write a program to implement a calculator using functions. Following operations should be implemented: a. Addition of two numbers b. Subtraction of two numbers c. Modulus of two number d. Multiplication of two numbers
def add_x_y(x, y):
print(f'{x} + {y} = {x + y}')
def sub_x_y(x, y):
print(f'{x} - {y} = {x - y}')
def mod_x_y(x, y):
if y == 0:
print('zero division')
else:
print(f'{x} / {y} = {x / y}')
def mul_x_y(x, y):
print(f'{x} * {y} = {x * y}')
def print_menu():
s = 'com - description\n'
s += 'a - add two numbers\n'
s += 'b - sub two numbers\n'
s += 'c - mul two numbers\n'
s += 'd - div two numbers\n'
s += 'e - exit'
print(s)
def main():
print_menu()
com = input('enter command: ').lower()
while com != 'e':
x = int(input('x = '))
y = int(input('y = '))
if com == 'a':
add_x_y(x, y)
elif com == 'b':
sub_x_y(x, y)
elif com == 'c':
mul_x_y(x, y)
elif com == 'd':
mod_x_y(x, y)
else:
print('incorrect command')
com = input('enter command: ').lower()
print('bye')
main()
Comments
Leave a comment