+: intermedios 111-117

This commit is contained in:
devfzn 2023-11-14 14:52:47 -03:00
parent 6e71ba65dd
commit 7f8905fd4f
Signed by: devfzn
GPG Key ID: E070ECF4A754FDB1
6 changed files with 222 additions and 33 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
__pycache__/ __pycache__/
interm/files/*.txt interm/files/*.txt
interm/files/*.csv

View File

@ -30,8 +30,10 @@ de *Nichola Lacey*
- [069-079](./interm/interm01.py) - [069-079](./interm/interm01.py)
- [080-087](./interm/interm02.py) - [080-087](./interm/interm02.py)
- [088-095](./interm/interm02.py) - [088-095](./interm/interm03.py)
- [096-104](./interm/interm02.py) - [096-104](./interm/interm04.py)
- [105-110](./interm/interm05.py)
- [111-117](./interm/interm06.py)
## Uso ## Uso

View File

@ -0,0 +1 @@
directorio para los archivos temporales de escritura y lectura

View File

@ -3,6 +3,7 @@ from . import interm02 as ex02
from . import interm03 as ex03 from . import interm03 as ex03
from . import interm04 as ex04 from . import interm04 as ex04
from . import interm05 as ex05 from . import interm05 as ex05
from . import interm06 as ex06
from common.common import ( from common.common import (
print_challenges, print_challenges,
user_input, user_input,
@ -165,34 +166,34 @@ def challenges05():
case _: case _:
continue continue
#def challenges06(): def challenges06():
# select_ok = False select_ok = False
# while not select_ok: while not select_ok:
# clear() clear()
# i, ex6 = print_challenges(ex06.basics_006) i, ex6 = print_challenges(ex06.interm006)
# selection = user_input(i) selection = user_input(i)
# match selection: match selection:
# case 1: case 1:
# run_func(ex6.over50) run_func(ex6.books)
# case 2: case 2:
# run_func(ex6.last_5) run_func(ex6.add_book)
# case 3: case 3:
# run_func(ex6.add_another) run_func(ex6.add_n_books)
# case 4: case 4:
# run_func(ex6.party) run_func(ex6.find_by_date)
# case 5: case 5:
# run_func(ex6.compnum) run_func(ex6.show_data)
# case 6: case 6:
# run_func(ex6.between) run_func(ex6.data_edit)
# case 7: case 7:
# run_func(ex6.bottles) run_func(ex6.maths_quiz)
# case 'v': case 'v':
# return return
# case 's': case 's':
# exit(0) exit(0)
# case _: case _:
# continue continue
#
#def challenges07(): #def challenges07():
# select_ok = False # select_ok = False
# while not select_ok: # while not select_ok:

185
interm/interm06.py Normal file
View File

@ -0,0 +1,185 @@
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)

View File

@ -70,7 +70,7 @@ def interm_challenges():
clear() clear()
print(content) print(content)
opcs_default(1) opcs_default(1)
selection = user_input(5) selection = user_input(6)
match selection: match selection:
case 1: case 1:
interm.challenges01() interm.challenges01()
@ -83,8 +83,7 @@ def interm_challenges():
case 5: case 5:
interm.challenges05() interm.challenges05()
case 6: case 6:
#interm.challenges06() interm.challenges06()
pass
case 7: case 7:
#interm.challenges07() #interm.challenges07()
pass pass