Question
add a function to get input from the user and to make sure that the user enters a valid integer from 1 to 100 for
add a function to get input from the user and to make sure that the user enters a valid integer from 1 to 100 for each value, and to handle exceptions if the user enters a non-numeric value
Use that function to get the input for each calculation
(please show me where to add or remove the code and show me the after result of the code)
# addition function def add(x, y): return x + y
# subtract function def subtract(x, y): return x - y
# multiply function def multiply(x, y): return x * y
# divide function def divide(x, y): return x / y
print("Select operation.") print("1.Addtion") print("2.Subtraction") print("3.Multiply") print("4.Dividion")
while True: # take input from the user choice = input("Enter choice(1/2/3/4): ")
# check if choice is one of the four options if choice in ('1', '2', '3', '4'): try: num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) except ValueError: print("Invalid input. Please enter a number.") continue
if choice == '1': print(num1, "+", num2, "=", add(num1, num2))
elif choice == '2': print(num1, "-", num2, "=", subtract(num1, num2))
elif choice == '3': print(num1, "*", num2, "=", multiply(num1, num2))
elif choice == '4': print(num1, "/", num2, "=", divide(num1, num2)) # check if user wants another calculation # break the while loop if answer is no next_calculation = input("Let's do next calculation? (y/n): ") if next_calculation == "n": break else: print("Invalid Input")
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