Advertisement
teslariu

temp ejemplo template while true

Aug 15th, 2023
973
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #!/usr/bin/env python
  4. # -*- coding: utf-8 -*-
  5. #
  6.  
  7. ### Ejemplo de template While True para resolver ejercicios
  8. def validar(escala):
  9.     while True:
  10.         try:
  11.             t = float(input(f"Ingrese la temperatura (en {escala}): "))
  12.         except ValueError:
  13.             print("Error, debe ingresar una temperatura válida")
  14.         else:
  15.             if escala == "K" and t > 0 or escala != "K":
  16.                 return t
  17.             else:
  18.                 print("La temperatura debe ser mayor a 0K")
  19.  
  20.  
  21. def menu():
  22.     print("""
  23.    Menu de opciones
  24.    ----------------
  25.    1. ºF a ºC
  26.    2. ºK a ºC
  27.    3. ºC a ºF
  28.    ----------------
  29.    4. Salir
  30.    """)
  31.     opcion = input("Ingrese una opción: ")
  32.     return opcion
  33.    
  34.    
  35. while True:
  36.     opcion = menu()
  37.    
  38.     if opcion == "1":
  39.         temp = validar("ºF")
  40.         temp = (temp - 32) / 1.8
  41.         print(f"{temp:.1f}ºC")
  42.        
  43.        
  44.     elif opcion == "2":
  45.         temp = validar("K")
  46.         temp = temp - 273
  47.         print(f"{temp:.1f}ºC")
  48.            
  49.    
  50.     elif opcion == "3":
  51.         temp = validar("ºC")
  52.         temp = temp * 1.8 + 32
  53.         print(f"{temp:.1f}ºF")
  54.        
  55.    
  56.     elif opcion == "4":
  57.         print("Gracias por utilizar este programa...")
  58.         break
  59.        
  60.     else:
  61.         print("Opción incorrecta...")
  62.    
  63.  
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement