186 lines
9.8 KiB
Python
186 lines
9.8 KiB
Python
|
from os import getcwd as pwd
|
||
|
import csv
|
||
|
|
||
|
class interm006:
|
||
|
|
||
|
def books(self):
|
||
|
"""Create a .csv file that will store the following data. Call it “Books.csv”.
|
||
|
- Book Author Year Released
|
||
|
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||
|
0 To Kill A Mockingbird Harper Lee 1960
|
||
|
1 A Brief History of Time Stephen Hawking 1988
|
||
|
2 The Great Gatsby F. Scott Fitzgerald 1922
|
||
|
3 The Man Who Mistook His Wife for a Hat Oliver Sacks 1985
|
||
|
4 Pride and Prejudice Jane Austen 1813 """
|
||
|
file_path = f"{pwd()}/interm/files/Books.csv"
|
||
|
books = ['To Kill A Mockingbird', 'A Brief History of Time',
|
||
|
'The Great Gatsby', 'The Man Who Mistook His Wife for a Hat',
|
||
|
'Pride and Prejudice']
|
||
|
authors = ['Harper Lee', 'Stephen Hawking', 'F. Scott Fitzgerald',
|
||
|
'Oliver Sacks', 'Jane Austen' ]
|
||
|
release = [ 1960, 1988, 1922, 1985, 1813 ]
|
||
|
with open(file_path, 'w') as file:
|
||
|
for i in range (5):
|
||
|
file.write(books[i]+','+authors[i]+','+str(release[i])+'\n')
|
||
|
print(f"\nPuedes encontrar el archivo en:\n'{file_path}'\n")
|
||
|
with open(file_path, 'r') as file:
|
||
|
reader = csv.reader(file)
|
||
|
contnt = list(reader)
|
||
|
print("- Book Author Year Released")
|
||
|
print("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
|
||
|
for i, line in enumerate(contnt):
|
||
|
print(str(i).ljust(3), line[0].ljust(40), line[1].ljust(21), line[2])
|
||
|
|
||
|
def add_book(self):
|
||
|
"""Using the Books.csv file from program 111, ask the user to enter
|
||
|
another record and add it to the end of the file. Display each row of
|
||
|
the .csv file on a separate line."""
|
||
|
file_path = f"{pwd()}/interm/files/Books.csv"
|
||
|
print("\nAgregando otro libro a la colección")
|
||
|
book = input("Ingresa el nombre del libro: ")
|
||
|
author = input("Ingresa el autor del libro: ")
|
||
|
date = input("Ingresa la fecha del libro: ")
|
||
|
new_data = [book, author, date]
|
||
|
with open(file_path, 'a') as file:
|
||
|
writer = csv.writer(file)
|
||
|
writer.writerow(new_data)
|
||
|
print(f"\nPuedes encontrar el archivo en:\n'{file_path}'\n")
|
||
|
with open(file_path, 'r') as file:
|
||
|
reader = csv.reader(file)
|
||
|
contnt = list(reader)
|
||
|
print("\n- Book "+
|
||
|
"Author Year Released")
|
||
|
print("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"+
|
||
|
"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
|
||
|
for i, line in enumerate(contnt):
|
||
|
print(str(i).ljust(3), line[0].ljust(40), line[1].ljust(21), line[2])
|
||
|
|
||
|
def add_n_books(self):
|
||
|
"""Using the Books.csv file, ask the user how many records they want to
|
||
|
add to the list and then allow them to add that many. After all the data
|
||
|
has been added, ask for an author and display all the books in the list
|
||
|
by that author. If there are no books by that author in the list,
|
||
|
display a suitable message."""
|
||
|
file_path = f"{pwd()}/interm/files/Books.csv"
|
||
|
to_add = int(input("¿Cuantos libros deseas ingresar?: "))
|
||
|
for i in range(to_add):
|
||
|
book = input(f"{i+1}) Ingresa el nombre del libro: ").title()
|
||
|
author = input(f"{i+1}) Ingresa el autor del libro: ").title()
|
||
|
date = input(f"{i+1}) Ingresa la fecha del libro: ")
|
||
|
new_data = [book, author, date]
|
||
|
with open(file_path, 'a') as file:
|
||
|
writer = csv.writer(file)
|
||
|
writer.writerow(new_data)
|
||
|
author = input("Ingresa un nombre de autor para mostrar sus libros: ").title()
|
||
|
with open(file_path, 'r') as file:
|
||
|
reader = csv.reader(file)
|
||
|
contnt = list(reader)
|
||
|
display = False
|
||
|
found = []
|
||
|
for raw in contnt:
|
||
|
if author in raw[1]:
|
||
|
display = True
|
||
|
found.append(raw)
|
||
|
if display:
|
||
|
print("\n- Book "+
|
||
|
"Author Year Released")
|
||
|
print("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
||
|
+"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
|
||
|
for i, line in enumerate(found):
|
||
|
print(str(i).ljust(3), line[0].ljust(40), line[1].ljust(21), line[2])
|
||
|
else:
|
||
|
print("Autor no encontrado")
|
||
|
|
||
|
def find_by_date(self):
|
||
|
"""Using the Books.csv file, ask the user to enter a starting year and
|
||
|
an end year. Display all books released between those two years."""
|
||
|
file_path = f"{pwd()}/interm/files/Books.csv"
|
||
|
year_ini = int(input("¿Desde que año quieres buscar?: "))
|
||
|
year_end = int(input("¿Hasta que año quieres buscar?: "))
|
||
|
with open(file_path, 'r') as file:
|
||
|
reader = csv.reader(file)
|
||
|
contnt = list(reader)
|
||
|
display = False
|
||
|
found = []
|
||
|
for raw in contnt:
|
||
|
if int(raw[2]) in range(year_ini, year_end+1):
|
||
|
display = True
|
||
|
found.append(raw)
|
||
|
if display:
|
||
|
print("\n- Book "+
|
||
|
"Author Year Released")
|
||
|
print("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
||
|
+"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
|
||
|
for i, line in enumerate(found):
|
||
|
print(str(i).ljust(3), line[0].ljust(40), line[1].ljust(21), line[2])
|
||
|
else:
|
||
|
print(f"No se encontraron libros entre {year_ini} y {year_end}")
|
||
|
|
||
|
def show_data(self):
|
||
|
"""Using the Books.csv file, display the data in the file along with the
|
||
|
row number of each"""
|
||
|
file_path = f"{pwd()}/interm/files/Books.csv"
|
||
|
with open(file_path, 'r') as file:
|
||
|
reader = csv.reader(file)
|
||
|
contnt = list(reader)
|
||
|
for i, line in enumerate(contnt):
|
||
|
print(str(i).ljust(3), line[0].ljust(40), line[1].ljust(21), line[2])
|
||
|
|
||
|
def data_edit(self):
|
||
|
"""Import the data from the Books.csv file into a list. Display the list
|
||
|
to the user. Ask them to select which row from the list they want to
|
||
|
delete and remove it from the list. Ask the user which data they want to
|
||
|
change and allow them to change it. Write the data back to the original
|
||
|
.csv file, overwriting the existing data with the amended data."""
|
||
|
file_path = f"{pwd()}/interm/files/Books.csv"
|
||
|
with open(file_path, 'r') as file:
|
||
|
reader = csv.reader(file)
|
||
|
contnt = list(reader)
|
||
|
for i, data in enumerate(contnt):
|
||
|
print(i, data)
|
||
|
del_book = int(input("¿Que fila deseas eliminar?: "))
|
||
|
contnt.remove(contnt[del_book])
|
||
|
for i, data in enumerate(contnt):
|
||
|
print(i, data)
|
||
|
edit_book = int(input("¿Que fila deseas editar?: "))
|
||
|
for i, data in enumerate(contnt[edit_book]):
|
||
|
print(i, data)
|
||
|
edit_field = int(input("¿Que campo deseas editar?: "))
|
||
|
resp = input("Ingresa el nuevo valor: ")
|
||
|
contnt[edit_book][edit_field] = resp
|
||
|
with open(file_path, 'w') as file:
|
||
|
writer = csv.writer(file)
|
||
|
writer.writerows(contnt)
|
||
|
print("\n- Book "+
|
||
|
"Author Year Released")
|
||
|
print("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"+
|
||
|
"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
|
||
|
for i, line in enumerate(contnt):
|
||
|
print(str(i).ljust(3), line[0].ljust(40), line[1].ljust(21), line[2])
|
||
|
|
||
|
def maths_quiz(self):
|
||
|
"""Create a simple maths quiz that will ask the user for their name and
|
||
|
then generate two random questions. Store their name, the questions they
|
||
|
were asked, their answers and their final score in a .csv file. Whenever
|
||
|
the program is run it should add to the .csv file and not overwrite
|
||
|
anything."""
|
||
|
from random import randint
|
||
|
file_path = f"{pwd()}/interm/files/Quiz.csv"
|
||
|
data = []
|
||
|
name = input("Ingresa tu nombre: ")
|
||
|
data.append(name)
|
||
|
points = 0
|
||
|
for _ in range(2):
|
||
|
num_1 = randint(1,100)
|
||
|
num_2 = randint(1,100)
|
||
|
question = f"Cuanto es {num_1} + {num_2}"
|
||
|
data.append(question)
|
||
|
resp = int(input(f"¿{question}?: "))
|
||
|
data.append(resp)
|
||
|
if resp == num_1+num_2:
|
||
|
points += 1
|
||
|
data.append(points)
|
||
|
with open(file_path, 'a') as file:
|
||
|
writer = csv.writer(file)
|
||
|
writer.writerow(data)
|