Objectivo
Desarrolla un programa Python que solicite al usuario dos números y muestre la división. Los errores se deben detectar utilizando "try..except".
Ejemplo Ejercicio Python
Mostrar Código Python
# Prompt the user for two numbers
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
# Try to perform the division and handle any potential errors
try:
result = num1 / num2
print(f"The result of {num1} divided by {num2} is {result}")
except ZeroDivisionError:
print("Error: Cannot divide by zero.")
except ValueError:
print("Error: Please enter valid numbers.")
Output
Case 1:
Enter the first number: 10
Enter the second number: 2
The result of 10.0 divided by 2.0 is 5.0
Case 2:
Enter the first number: ten
Enter the second number: 2
Error: Please enter valid numbers.
Case 3:
Enter the first number: ten
Enter the second number: 2
Error: Please enter valid numbers.
Código de Ejemplo copiado
Comparte este ejercicios Python