QUESTION IMAGE
Question
write a program that asks the user for 2 numbers and a mathematical expression (addition, subtraction, multiplication, or division) and displays the answer when the calculation is performed on the 2 given numbers. use a check_number function to make sure the user has entered a number. if not, tell them that 1 will be used as default. use a check_symbol function to make sure the user has entered a valid response. if not, tell them that.
To solve this problem, we will create a Python program that takes two numbers and a mathematical operation (addition, subtraction, multiplication, or division) from the user, validates the inputs, and then performs the calculation. We will use two helper functions: check_number to validate the numbers and check_symbol to validate the mathematical operation.
Approach
check_numberfunction: This function will attempt to convert the user's input to a float. If the conversion is successful, it returns the number. If not, it returns 1 (the default value) and notifies the user.check_symbolfunction: This function will check if the user's input is one of the valid mathematical operations (+,-,*,/). If it is, it returns the symbol. If not, it notifies the user and asks for a valid input.- Main Program: The main part of the program will:
- Prompt the user for two numbers, using the
check_numberfunction to validate each input. - Prompt the user for a mathematical operation, using the
check_symbolfunction to validate the input. - Perform the calculation based on the validated operation and display the result.
Solution Code
def check_number(prompt):
while True:
try:
num = float(input(prompt))
return num
except ValueError:
print("That's not a valid number. 1 will be used as default.")
return 1.0
def check_symbol():
valid_symbols = ['+', '-', '*', '/']
while True:
symbol = input("Enter the mathematical expression (+, -, *, /): ")
if symbol in valid_symbols:
return symbol
else:
print("That's not a valid mathematical expression. Please enter +, -, *, or /.")
# Get the first number
num1 = check_number("Enter the first number: ")
# Get the second number
num2 = check_number("Enter the second number: ")
# Get the mathematical symbol
symbol = check_symbol()
# Perform the calculation
if symbol == '+':
result = num1 + num2
elif symbol == '-':
result = num1 - num2
elif symbol == '*':
result = num1 * num2
else: # symbol == '/'
if num2 == 0:
print("Division by zero is not allowed.")
else:
result = num1 / num2
# Display the result (if division by zero didn't occur)
if symbol != '/' or (symbol == '/' and num2 != 0):
print(f"{num1} {symbol} {num2} = {result}")check_numberfunction:
- This function uses a
try-exceptblock to attempt to convert the user's input to a float. If the conversion is successful (i.e., the input is a valid number), it returns the number. If aValueErroris raised (indicating the input is not a valid number), it prints a message and returns 1.0 (the default value).
check_symbolfunction:
- This function maintains a list of valid mathematical symbols (
+,-,*,/). It repeatedly prompts the user for an operation until a valid symbol is entered. If the input is valid, it returns the symbol; otherwise, it prints an error message and asks again.
- Main Program:
- The program first calls
check_numbertwice to get two valid numbers (or default to 1 if invalid). - It then calls
check_symbolto get a valid mathematical operation. - Based on the validated operation, it performs the corresponding calculation. For division, it also checks if the second number is zero to avoid division by zero errors.
- Finally, it prints the result of the calculation, ensuring that division by zero errors are handled appropriately.
This approach ensures that the user's inputs are validated, and the program can handle both valid and invalid inputs gracefully, providing clear feedback and using default values when necessary.
Snap & solve any problem in the app
Get step-by-step solutions on Sovi AI
Photo-based solutions with guided steps
Explore more problems and detailed explanations
check_numberfunction:
- This function uses a
try-exceptblock to attempt to convert the user's input to a float. If the conversion is successful (i.e., the input is a valid number), it returns the number. If aValueErroris raised (indicating the input is not a valid number), it prints a message and returns 1.0 (the default value).
check_symbolfunction:
- This function maintains a list of valid mathematical symbols (
+,-,*,/). It repeatedly prompts the user for an operation until a valid symbol is entered. If the input is valid, it returns the symbol; otherwise, it prints an error message and asks again.
- Main Program:
- The program first calls
check_numbertwice to get two valid numbers (or default to 1 if invalid). - It then calls
check_symbolto get a valid mathematical operation. - Based on the validated operation, it performs the corresponding calculation. For division, it also checks if the second number is zero to avoid division by zero errors.
- Finally, it prints the result of the calculation, ensuring that division by zero errors are handled appropriately.
This approach ensures that the user's inputs are validated, and the program can handle both valid and invalid inputs gracefully, providing clear feedback and using default values when necessary.