Advertisement
teslariu

conv tempo mejorado

Jan 12th, 2023
1,104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. # Script que permite convertir temperaturas de ºF a ºC y viceversa
  2. # En esta versión vamos a incluir funciones y manejo de excepciones
  3.  
  4. def menu():
  5.     return"""
  6.    Menu de opciones
  7.    ----------------
  8.    1. ºC --> ºF
  9.    2. ºF --> ºC
  10.    3. Salir
  11.    ---------------
  12.    """
  13.  
  14.  
  15. def ingresar_temperatura():
  16.     while True:
  17.         try:
  18.             temp = float(input("Ingrese la temperatura: "))
  19.         except ValueError:
  20.             print("Error, debe ingresar un número")
  21.         else:
  22.             return temp
  23.  
  24.  
  25. def pasar_a_C(temp):
  26.     temp = (temp - 32) / 1.8
  27.     return f"La temperatura es {temp:.1f}ºC"
  28.  
  29.  
  30. def pasar_a_F(temp):
  31.     temp = (temp * 1.8) + 32
  32.     return f"La temperatura es {temp:.1f}ºF"
  33.  
  34.  
  35.  
  36. print("Conversor de temperaturas")
  37.  
  38. while True:
  39.     print(menu())
  40.     opcion = input("Seleccione su opción: ")
  41.  
  42.     if opcion == "1":
  43.         temp = ingresar_temperatura()
  44.         print(pasar_a_F(temp))
  45.  
  46.    
  47.     elif opcion == "2":
  48.         temp = ingresar_temperatura()
  49.         print(pasar_a_C(temp))
  50.        
  51.    
  52.     elif opcion == "3":
  53.         print("Hasta luego...")
  54.         break
  55.    
  56.     else:
  57.         print("Opción incorrecta...")
  58.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement