Answered step by step
Verified Expert Solution
Question
1 Approved Answer
how to fix this python code Please enter the mathematical expression ( ' number 1 ' operator 'number 2 ' ) : 2 / 0
how to fix this python code Please enter the mathematical expression number operator 'number :
Invalid input. Divison by is not permissible.
this is code use
# Step : Accept Input
def getinput:
expr inputPlease enter the mathematical expression number operator 'number :
return expr
# Step : Parse Input
def parseinputexpr:
try:
opr oprtr, opr expr.split
return floatopr oprtr, floatopr
except ValueError:
return None, None, None
# Step : Validate Input
def validateopr oprtr, opr:
validoperators
if oprtr not in validoperators:
return False
return True
# Step : Perform Calculation
def calculateopr oprtr, opr:
if oprtr :
return opr opr
elif oprtr :
return opr opr
elif oprtr :
return opr opr
elif oprtr :
if opr:
return floatinf # Return infinity for division by zero
return opr opr
# Step : Handle Errors
def handleresult:
if result is None:
printError: Division by zero"
return True
return False
# Step : Print Result
def printresultresult:
printResult: result
# Step : Repeat or Quit
def askrepeat:
response inputDo you want to perform another operation? yn: lower
return response.startswithy
# Main Function
def main:
while True:
expr getinput
opr oprtr, opr parseinputexpr
if not opr or not oprtr or not opr:
printInvalid input. Division by is not permissible."
continue
if not validateopr oprtr, opr:
printInvalid operator. Please enter a valid operator
continue
result calculateopr oprtr, opr
if handleresult:
continue
printresultresult
if not askrepeat:
break
if namemain:
main
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