diff --git a/.gitignore b/.gitignore index c18dd8d..8705cca 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ __pycache__/ +interm/files/*.txt diff --git a/README.md b/README.md index 4c5a6df..b15ad28 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,8 @@ de *Nichola Lacey* - [069-079](./interm/interm01.py) - [080-087](./interm/interm02.py) +- [088-095](./interm/interm02.py) +- [096-104](./interm/interm02.py) ## Uso diff --git a/interm/files/text_files_temp_dir b/interm/files/text_files_temp_dir new file mode 100644 index 0000000..e69de29 diff --git a/interm/interm.py b/interm/interm.py index cb71b0d..3666fc6 100644 --- a/interm/interm.py +++ b/interm/interm.py @@ -2,6 +2,7 @@ from . import interm01 as ex01 from . import interm02 as ex02 from . import interm03 as ex03 from . import interm04 as ex04 +from . import interm05 as ex05 from common.common import ( print_challenges, user_input, @@ -138,40 +139,32 @@ def challenges04(): case _: continue -#def challenges05(): -# select_ok = False -# while not select_ok: -# clear() -# i, ex5 = print_challenges(ex05.basics_005) -# selection = user_input(i) -# match selection: -# case 1: -# run_func(ex5.name_three) -# case 2: -# run_func(ex5.name_num) -# case 3: -# run_func(ex5.name_nl) -# case 4: -# run_func(ex5.name_nl_num) -# case 5: -# run_func(ex5.times_table) -# case 6: -# run_func(ex5.count_down) -# case 7: -# run_func(ex5.mambo_name) -# case 8: -# run_func(ex5.may_add) -# case 9: -# run_func(ex5.up_or_down) -# case 10: -# run_func(ex5.guests) -# case 'v': -# return -# case 's': -# exit(0) -# case _: -# continue -# +def challenges05(): + select_ok = False + while not select_ok: + clear() + i, ex5 = print_challenges(ex05.interm005) + selection = user_input(i) + match selection: + case 1: + run_func(ex5.names_txt) + case 2: + run_func(ex5.names_txt) + case 3: + run_func(ex5.read_names) + case 4: + run_func(ex5.add_name) + case 5: + run_func(ex5.menu) + case 6: + run_func(ex5.names2) + case 'v': + return + case 's': + exit(0) + case _: + continue + #def challenges06(): # select_ok = False # while not select_ok: diff --git a/interm/interm05.py b/interm/interm05.py new file mode 100644 index 0000000..5817f6b --- /dev/null +++ b/interm/interm05.py @@ -0,0 +1,108 @@ +from os import getcwd as pwd + +class interm005: + + def nums_txt(self): + """Write a new file called \"Numbers.txt\". Add five numbers to the + document which are stored on the same line and only separated by a + comma. Once you have run the program, look in the location where your + program is stored and you should see that the file has been created.""" + from random import randint + file_path = f"{pwd()}/interm/files/Numbers.txt" + with open(file_path, 'w') as file: + for i in range(5): + if i < 4: + file.write(str(randint(0,100))+', ') + else: + file.write(str(randint(0,100))) + print(f"Puedes encontrar el archivo en '{file_path}'") + + def names_txt(self): + """Create a new file called \"Names.txt\". Add five names to the + document, which are stored on separate lines. Once you have run the + program, look in the location where your program is stored and check + that the file has been created properly.""" + file_path = f"{pwd()}/interm/files/Names.txt" + names = ( 'Juana', 'Roberta', 'Manuelo', 'Genialo', 'Zerafin' ) + with open(file_path, 'w') as file: + file.writelines('\n'.join(names)) + file.write('\n') + print(f"Puedes encontrar el archivo en '{file_path}'") + + def read_names(self): + """Open the \"Names.txt\" file and display the data in Python.""" + file_path = f"{pwd()}/interm/files/Names.txt" + with open(file_path, 'r') as file: + print(file.read()) + + def add_name(self): + """Open the \"Names.txt\" file. Ask the user to input a new name. Add + this to the end of the file and display the entire file.""" + file_path = f"{pwd()}/interm/files/Names.txt" + name = input("Ingresa un nombre para agregarlo al archivo 'Names.txt': ") + with open(file_path, 'a') as file: + file.write(name+'\n') + self.read_names() + + def menu(self): + """Display the following menu to the user: + 1) Create a new file + 2) Display the file + 3) Add a new item to the file + + Make a selection 1, 2 or 3: + Ask the user to enter 1, 2 or 3. If they select anything other than 1, + 2 or 3 it should display a suitable error message. If they select 1, + ask the user to enter a school subject and save it to a new file called + \"Subject.txt\". It should overwrite any existing file with a new file. + If they select 2, display the contents of the \"Subject.txt\" file. If + they select 3, ask the user to enter a new subject and save it to the + file and then display the entire contents of the file. Run the program + several times to test the options.""" + file_path = f"{pwd()}/interm/files/Subject.txt" + msg = "Ingresa una asignatura para agregar al archivo 'Subject.txt': " + menu = """ + 1) Crear un nuevo archivo + 2) Mostrar el contenido del archivo + 3) Agregar un nuevo elemento al archivo + """ + print(menu) + sel = int(input(" Ingresa una opción (1-3): ")) + match sel: + case 1: + item = input(msg) + with open(file_path, 'w') as file: + file.write(item+'\n') + print(f"Puedes encontrar el archivo en '{file_path}'") + case 2: + with open(file_path, 'r') as file: + print(file.read()) + case 3: + item = input(msg) + with open(file_path, 'a') as file: + file.write(item+'\n') + with open(file_path, 'r') as file: + print(file.read()) + case _: + print("Debes ingresar una opción válida") + + def names2(self): + """Using the \"Names.txt\" file you created earlier, display the + list of names in Python. Ask the user to type in one of the names + and then save all the names except the one they entered into a new + file called \"Names2.txt\".""" + file_path1 = f"{pwd()}/interm/files/Names.txt" + file_path2 = f"{pwd()}/interm/files/Names2.txt" + names = [] + with open(file_path1, 'r') as file: + names = file.readlines() + for name in names: + print(name.replace('\n','')) + sel = input("Ingresa el nombre a excluir en \"Names2.txt\": ")+'\n' + with open(file_path2, 'w') as file: + for name in names: + if sel != name: + file.write(name) + print(f"\nPuedes encontrar el archivo en '{file_path2}'\n") + with open(file_path2, 'r') as file: + print(file.read()) diff --git a/main.py b/main.py index d018267..9aee51e 100755 --- a/main.py +++ b/main.py @@ -70,7 +70,7 @@ def interm_challenges(): clear() print(content) opcs_default(1) - selection = user_input(4) + selection = user_input(5) match selection: case 1: interm.challenges01() @@ -81,8 +81,7 @@ def interm_challenges(): case 4: interm.challenges04() case 5: - #interm.challenges05() - pass + interm.challenges05() case 6: #interm.challenges06() pass