Objectivo
Desarrolla un programa Python que solicite al usuario dos números y utilice el operador condicional (?) para determinar lo siguiente:
Si el primer número es positivo
Si el segundo número es positivo
Si ambos son positivos
¿Cuál es menor?
Ejemplo Ejercicio Python
Mostrar Código Python
# Ask the user for two numbers
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
# Check if the first number is positive
result_num1 = "The first number is positive." if num1 > 0 else "The first number is not positive."
# Check if the second number is positive
result_num2 = "The second number is positive." if num2 > 0 else "The second number is not positive."
# Check if both numbers are positive
result_both = "Both numbers are positive." if num1 > 0 and num2 > 0 else "Both numbers are not positive."
# Determine which number is smaller
smaller = num1 if num1 < num2 else num2
# Print the results
print(result_num1)
print(result_num2)
print(result_both)
print(f"The smaller number is: {smaller}")
Output
Case 1:
Enter the first number: 5
Enter the second number: -3
The first number is positive.
The second number is not positive.
Both numbers are not positive.
The smaller number is: -3.0
Case 2:
Enter the first number: -2
Enter the second number: 8
The first number is not positive.
The second number is positive.
Both numbers are not positive.
The smaller number is: -2.0
Código de Ejemplo copiado
Comparte este ejercicios Python