Question
Simple Calculator: In Python, implement a simple calculator that does the following operations: summation, subtraction, multiplication, division, mod, power, exp, natural log and abs. a)
Simple Calculator: In Python, implement a simple calculator that does the following operations: summation, subtraction, multiplication, division, mod, power, exp, natural log and abs.
a) Follow the instructions below: To work with the calculator, the user is asked to enter the first number, then the operation, and finally, second number if required. Your code has to recognize the need for the second number and ask for it if required. After performing one operation, the calculator prints the output of the operation. After performing one operation, the calculator must not exit. It has to start again and ready for the next operation. The calculator will be closed if the user writes x as any input. Use functions to perform the operations and the appropriate conditions to prevent common errors such as entering characters as one of the numbers etc. b) Run your code and provide the results for at least one example per operation.
I have already created the majority of this calculator I just need to fix the function checkInp if anyone could help me.. thank you. My code:
import math import sys
# Problem 1a)
def checkInp(s): try: userInput = (input(s)) str(s) if s=='x': sys.exit(0) int(s) except ValueError: print("Not an integer! Re-enter: ") continue return s
while input("Press any key, then enter to start Calculator..."): a = checkInp(input("Enter the first number: "))
print("Available Operations") print("1.Addition") print("2.Subtraction") print("3.Multiplication") print("4.Division") print("5.Modulus") print("6.Power") print("7.Exponent") print("8.Natural Log") print("9.Absolute Value") print("Enter x to exit")
choice = checkInp(input("Enter operation number: "))
if choice=='7' or choice=='8' or choice=='9': if choice=='7': print(math.exp(a)) elif choice=='8': print(math.log(a)) elif choice=='9': print(abs(a)) else: y = checkInp(input("Enter the second number: "))
if choice=='1': print(a+y) elif choice=='2': print(a-y) elif choice=='3': print(a*y) elif choice=='4': while y==0: print("Cannot divide by 0") y = checkInp(input("Re-enter second number: ")) print(a/y) elif choice=='5': print(a%y) elif choice=='6': print(math.pow(a,y)) else: print("Your input was not acceptable.. please start over..")
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started