Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem Description: Write an interactive application in Python to mimic a calcu

ID: 3604584 • Letter: P

Question

Problem Description: Write an interactive application in Python to mimic a calculator. In the first assignment you were asked to output the sum, difference, product and quotient of two numbers. In this assignment you will ask the user for their choice of operator (+, ,*,/), and output the corresponding answer. If the second number is 0 for the division operator, you must output an error message saying that division by 0 is not possible. Inputs It should take as input 2 integers and a character for the operator. Output It should check for the operator and output the result. If it is the division operator, it should check to see if the second number is 0 and output an error message and quit the program.

Explanation / Answer

def add(x, y): return x + y
def subtract(x, y): return x - y
def multiply(x, y): return x * y
def divide(x, y): return x / y
  
choice = input("Enter which operation you have to perform:")
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

if choice is '+' : print(num1,"+",num2,"=", add(num1,num2))

elif choice is '-' : print(num1,"-",num2,"=", subtract(num1,num2))
elif choice is '*' : print(num1,"*",num2,"=", multiply(num1,num2))
elif (choice is '/') and (num2 is 0 ): print("division by zero is not possible")
elif (choice is '/'): print(num1,"/",num2,"=", divide(num1,num2))

else: print("Invalid operator")

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote