Código en Python para convertir temperaturas entre Celsius y Fahrenheit
El programa pide al usuario que ingrese una temperatura en Celsius o Fahrenheit y la convierte a la otra unidad utilizando las fórmulas:
- De Celsius a Fahrenheit:
- De Fahrenheit a Celsius:
Ejemplo en Python
python
def celsius_a_fahrenheit(celsius):
return (celsius * 9/5) + 32
def fahrenheit_a_celsius(fahrenheit):
return (fahrenheit - 32) * 5/9
# Solicitar al usuario la opción de conversión
opcion = input("Elige una opción:\n1. Celsius a Fahrenheit\n2. Fahrenheit a Celsius\n")
if opcion == '1':
celsius = float(input("Ingresa la temperatura en Celsius: "))
print(f"{celsius}°C = {celsius_a_fahrenheit(celsius):.2f}°F")
elif opcion == '2':
fahrenheit = float(input("Ingresa la temperatura en Fahrenheit: "))
print(f"{fahrenheit}°F = {fahrenheit_a_celsius(fahrenheit):.2f}°C")
else:
print("Opción no válida.")
Espero que haya ayudado, bye ??