EmaSMach

Ejemplo simple de una calculadora en python

Jul 9th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. # Operaciones
  2. # Muestra una simple manera de implementar una calculadora en python.
  3. # Necesita mucha mejora, validaciones, etc. Pero la idea es solo mostrar la lógica.
  4. # Author: David Emanuel Sandoval
  5.  
  6. def sumar():
  7.     a = int(input("Primer numero: ")) # pasamos el valor ingresado a entero
  8.     b = int(input("Segundo numero: "))
  9.     resultado = a + b
  10.     print(resultado)
  11.     return a + b
  12.  
  13. def restar():
  14.     # agregar código
  15.     print(resultado)
  16.     return a - b
  17.  
  18.  
  19. # Menú de opciones
  20.  
  21. def mostrar_menu():
  22.     opciones = """
  23.        1) Sumar
  24.        2) Restar
  25.        3) Muliplicar
  26.        .........etc
  27.        0) Salir
  28.        """
  29.     print(opciones)
  30.  
  31. def preguntar_seguir():
  32.     print("¿Desea seguir?")
  33.     respuesta = input("Presione 's' para sí, o 'n' para no :")
  34.     if respuesta == 's':
  35.         return True
  36.     elif respuesta == 'n':
  37.         return False
  38.  
  39.    
  40. def bucle_principal():
  41.     while True:
  42.         mostrar_menu()
  43.         eleccion = input("Elegir una opcion: ")
  44.         if eleccion == '1':
  45.             sumar()
  46.             if preguntar_seguir() == True:
  47.                 continue
  48.             elif preguntar_seguir() == False:
  49.                 break
  50.             else:
  51.                 print("Opcion desconocida")
  52.                 continue # Volvemos al inicio
  53.         elif eleccion == '2':
  54.             restar()
  55.             if preguntar_seguir():
  56.                 continue
  57.             else:
  58.                 break
  59.         elif eleccion == 'algun otro numero':
  60.             alguna_otra_funcion()
  61.             # preguntar
  62.         elif eleccion == '0':
  63.             break # salimos del bucle
  64.         else:
  65.             print('Opcion inválida') # Y regresa al inicio
  66.            
  67.  
  68. if __name__ == '__main__':
  69.     bucle_principal()
Advertisement
Add Comment
Please, Sign In to add comment