feat(problem-04.py): completed problem

This commit is contained in:
baphim
2026-09-13 08:48:37 -04:00
parent 1e4e0fe72d
commit d271ab4b4b
+14 -7
View File
@@ -1,15 +1,22 @@
# Write a Python program that prompts the user to input two numbers and raises a TypeError exception if the inputs are # Write a Python program that prompts the user to input two numbers and raises a TypeError exception if the inputs are
# not numerical. # not numerical.
# Define the function to check if user input is a number
def numb_check(n): def numb_check(n):
while True: # Try if input is number and store in numb variable
try: try:
numb = float(input(n)) numb = float(n)
except ValueError: # Return the stored number
print(f"{numb} is not a number") return numb
except ValueError:
# Handle a non-numeric input as to pass it on to the * operation
print('Enter only numbers...')
# Call the function and store the input in x variable
x = numb_check(input('Enter a number: ')) x = numb_check(input('Enter a number: '))
# Call the function and store this input in y variable
y = numb_check(input('Enter another number: ')) y = numb_check(input('Enter another number: '))
# Multiply the two inputs as to raise a TypeError if input is non-numeric
product = x * y product = x * y
print(product) # Print the product of the multiplication
print(f'{x} multiplied by {y} is {product}')