In: Computer Science
python code
Write a simple calculator:
This program must have 9 functions:
•main() Controls the flow of the program (calls the other modules)
•userInput() Asks the user to enter two numbers
•add() Accepts two numbers, returns the sum
•subtract() Accepts two numbers, returns the difference of the first number minus the second number
•multiply() Accepts two numbers, returns the product
•divide() Accepts two numbers, returns the quotient of the first number divided by the second number
•modulo() Accepts two numbers, returns the modulo of the first number divided by the second number
•exponent() Accepts two numbers, returns the first number raised to the power of the second number.
•userOutput() Gives a user friendly output of all the calculations.
NOTE: The ONLY place that you will print results is inside the userOutput function
with any numbers entered. Your program must print an appropriate message if the user enters anything other than two integers. (Hint: Try...except)
from math import *
def main():
a,b=userInput()
r=add(a,b)
userOutput(r,"addition")#call the methods and print the
results
r=subtract(a,b)
userOutput(r,"subtract")
r=multiply(a,b)
userOutput(r,"multiply")
r=divide(a,b)
userOutput(r,"divide")
r=modulo(a,b)
userOutput(r,"modulo")
r=exponent(a,b)
userOutput(r,"exponent")
def userInput():
try:
n1=int(input("Enter number 1:"))
n2=int(input("Enter number 2:"))#read numbers
except:
print("Enter valid numbers")
return n1,n2#return numbers
def add(a,b):#functions that compute result and return the values
to main function
return a+b
def subtract(a,b):
return a-b
def multiply(a,b):
return a*b
def divide(a,b):
return a/b
def modulo(a,b):
return a%b
def exponent(a,b):
return pow(a,b)
def userOutput(res,op):#prints the output result to the user
print("The ",op," result of two numbers is ",res)
main()#call the main function
Screenshots:
The screenshots are attached below for reference.
Please follow them for output and proper indentation.
Please upvote my answer. Thank you.