Answer to Question #283836 in Python for hyper

Question #283836

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


1
Expert's answer
2021-12-30T15:17:27-0500
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()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS