Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #
- """
- ========= ===============================================================
- Character Meaning
- --------- ---------------------------------------------------------------
- 'r' open for reading (default)
- 'w' open for writing, truncating the file first
- 'x' create a new file and open it for writing
- 'a' open for writing, appending to the end of the file if it exists
- 'b' binary mode
- 't' text mode (default)
- '+' open a disk file for updating (reading and writing)
- 'U' universal newline mode (deprecated)
- ========= ===============================================================
- The default mode is 'rt' (open for reading text). For binary random
- access, the mode 'w+b' opens and truncates the file to 0 bytes, while
- 'r+b' opens the file without truncation. The 'x' mode implies 'w' and
- raises an `FileExistsError` if the file already exists.
- # Script que crea un archivo con temperaturas expresadas en ºC y a partir
- # de él, genera otro archivo pero con las temperaturas en ºF
- # creo un archivo vacío y le agrego datos
- try:
- f = open("celsius.txt","x")
- except FileExistsError:
- f = open("celsius.txt","a")
- # creo una lista con los datos a guardar
- datos = ["12ºC\n", "-2.5ºC\n", "11ºC\n", "0ºC\n"]
- # escribo los datos en el archivo y lo cierro
- f.writelines(datos)
- f.close()
- # abro el archivo nuevamente porque me olvidé de agregar un dato
- # with cierra automáticamente, no necesito close()
- with open("celsius.txt","a") as f:
- f.write("10ºC\n")
- # creo el archivo farenheit:
- try:
- f2 = open("farenheit.txt","x")
- except FileExistsError:
- f2 = open("farenheit.txt","a")
- f = open("celsius.txt")
- # itero linea a linea celsius, convierto a ºF y la grabo
- for temp in f.readlines():
- valor, _ = temp.split("º")
- valor = float(valor) * 1.8 + 32
- f2.write(f"{valor:.1f}ºF\n")
- # cierro archivos
- f.close()
- f2.close()
- """
- # Script que calcula las raices de una ec. cuadratica en el campo de
- # de los nros reales
- # Ej:
- # >> Ingrese el termino cuadratico a: 1
- # >> Ingrese el término lineal b: 10
- # >> Ingrese el término independiente c: 5
- # >> No existen raíces
- # NOTA: el término cuadrático debe ser distinto de cero
- from math import sqrt
- from datetime import datetime
- def ingresar_datos(termino):
- while True:
- try:
- n = float(input(f"Ingrese el término {termino}: "))
- except ValueError:
- print("Debe ingresar un número ")
- else:
- if termino == "cuadratico" and n != 0:
- return n
- elif termino != "cuadratico":
- return n
- else:
- print("Debe ingresar un número distinto de cero")
- def calcular_raices(a,b,c):
- delta = b**2 - 4*a*c
- if not delta:
- raiz1 = -1*b / (2*a)
- return raiz1
- elif delta > 0:
- raiz1 = (-1*b + sqrt(delta)) / (2*a)
- raiz2 = (-1*b - sqrt(delta)) / (2*a)
- return [raiz1, raiz2]
- else:
- return "No existen raices en el campo de los nros reales"
- def guardar_datos(a,b,c,raices):
- # escribo fecha y hora y los parametros de la parabola
- try:
- f = open("cuadratica.txt","x")
- except FileExistsError:
- f = open("cuadratica.txt","a")
- f.write(f"{datetime.now().strftime('%d/%m/%y %H:%M:%S')}\n")
- f.write("Parámetros de la parábola\n")
- f.write(f"Término cuadrático a = {a}\n")
- f.write(f"Término lineal b = {b}\n")
- f.write(f"Término independiente c = {c}\n")
- if isinstance(raices, float):
- f.write(f"Raíz única: {raices:.2f}\n")
- elif isinstance(raices, list):
- f.write(f"Raíz 1 = {raices[0]} - Raiz 2 = {raices[1]}\n")
- else:
- f.write(f"{raices}\n")
- f.write(f"\n------------------------------------------------------\n\n")
- def imprimir_raices(raices):
- if isinstance(raices, float):
- print(f"Raíz única: {raices}\n")
- elif isinstance(raices, list):
- print(f"Raíz 1 = {raices[0]} - Raiz 2 = {raices[1]}\n")
- else:
- print(raices)
- def menu():
- print("""
- Menu de opciones
- ------------------
- 1. Calcular raices
- 2. Salir
- ------------------
- """)
- op = input("Seleccione una opcion: ")
- return op
- while True:
- opcion = menu()
- if opcion == "1":
- a = ingresar_datos("cuadratico")
- b = ingresar_datos("lineal")
- c = ingresar_datos("independiente")
- raices = calcular_raices(a,b,c)
- guardar_datos(a,b,c,raices)
- imprimir_raices(raices)
- elif opcion == "2":
- print("Hasta luego...")
- break
- else:
- print("Opcion incorrecta")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement