Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #
- """
- Script que calcula las raices de una ec cuadratica
- """
- from datetime import datetime
- from math import sqrt
- def ingresar_dato(tipo):
- while True:
- try:
- n = float(input(f"Ingrese el coeficiente {tipo}: "))
- except ValueError:
- print("No ha ingresado un número")
- else:
- if tipo == "a" and not n:
- print("El coeficiente a debe ser no nulo")
- continue
- return n
- def calcular_raices(a,b,c):
- delta = b**2 - 4*a*c
- if delta == 0:
- raiz = -1*b / (2*a)
- return raiz
- elif delta > 0:
- raiz1 = (-1*b + sqrt(delta)) / (2*a)
- raiz2 = (-1*b - sqrt(delta)) / (2*a)
- return [raiz1, raiz2]
- else:
- return "No hay raices"
- def imprimir_raices(raices):
- if isinstance(raices, float):
- print("Existe una solución: {:.2f}".format(raices))
- elif isinstance(raices, list):
- print("Existen dos soluciones: {:.2f} y {:.2f}".format(raices[0], raices[1]))
- else:
- print(raices)
- def grabar_datos(a,b,c,raices):
- # abro el archivo en modo append, si no existe lo crea
- f = open("resultados.txt","a")
- f.write("\n" + 25*"=" + "\n")
- f.write(datetime.now().strftime("%d/%m/%y %H:%M:%S") + "\n")
- f.write("Parametros:\n")
- f.write(f"a: {a}\n")
- f.write(f"b: {b}\n")
- f.write(f"c: {c}\n")
- if isinstance(raices, float):
- f.write(f"Existe una solución: {raices:.2f}\n")
- elif isinstance(raices, list):
- f.write(f"Existen dos soluciones: {raices[0]:.2f} y {raices[1]:.2f}\n")
- else:
- f.write(f"{raices}\n")
- f.close()
- while True:
- print("\nScript para calcular raices de una ec. cuadrática")
- a = ingresar_dato("a")
- b = ingresar_dato("b")
- c = ingresar_dato("c")
- raices = calcular_raices(a,b,c)
- imprimir_raices(raices)
- grabar_datos(a,b,c,raices)
- opcion = input("Presione cualquier tecla o '1' para salir: ")
- if opcion == "1":
- print("Gracias por usar este programa...")
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement