298 lines
11 KiB
Python
298 lines
11 KiB
Python
class interm007:
|
||
|
||
def sub_118(self):
|
||
"""Define a subprogram that will ask the user to enter a number and
|
||
save it as the variable \'num\'. Define another subprogram that will
|
||
use \'num\' and count from 1 to that number."""
|
||
def save_num():
|
||
return int(input("Ingresa un número: "))
|
||
|
||
def use_num(num):
|
||
print(*[x for x in range(1, num+1)])
|
||
|
||
num = save_num()
|
||
use_num(num)
|
||
|
||
def sub_119(self):
|
||
"""Define a subprogram that will ask the user to pick a low and a high
|
||
number, and then generate a random number between those two values and
|
||
store it in a variable called \'comp_num\'.
|
||
Define another subprogram that will give the instruction \'I am thinking
|
||
of a number…\' and then ask the user to guess the number they are
|
||
thinking of.
|
||
Define a third subprogram that will check to see if the comp_num is the
|
||
same as the user’s guess. If it is, it should display the message
|
||
\'Correct, you win\', otherwise it should keep looping, telling the user
|
||
if they are too low or too high and asking them to guess again until they
|
||
guess correctly."""
|
||
from random import randint
|
||
def num_gen():
|
||
ini = int(input("Ingresa un número inicial: "))
|
||
end = int(input("Ingresa un número final: "))
|
||
return randint(ini, end)
|
||
|
||
def thinking():
|
||
print("¡Estoy pensando en un número!")
|
||
guess = int(input("¿Cual es el número?: "))
|
||
return guess
|
||
|
||
def check_nums(num, guess):
|
||
while num != guess:
|
||
if num > guess:
|
||
print("Muy bajo")
|
||
else:
|
||
print("Muy alto")
|
||
guess = int(input("¿Cual es el número?: "))
|
||
print("Correcto, ganaste!")
|
||
|
||
secret = num_gen()
|
||
guess = thinking()
|
||
check_nums(secret, guess)
|
||
|
||
def sub_120(self):
|
||
"""Display the following menu to the user:
|
||
1) Addition
|
||
2) Subtraction
|
||
Enter 1 or 2:
|
||
If they enter a 1, it should run a subprogram that will generate two
|
||
random numbers between 5 and 20, and ask the user to add them together.
|
||
Work out the correct answer and return both the user’s answer and the
|
||
correct answer.
|
||
If they entered 2 as their selection on the menu, it should run a
|
||
subprogram that will generate one number between 25 and 50 and another
|
||
number between 1 and 25 and ask them to work out num1 minus num2. This
|
||
way they will not have to worry about negative answers. Return both the
|
||
user’s answer and the correct answer.
|
||
Create another subprogram that will check if the user’s answer matches
|
||
the actual answer. If it does, display \'Correct\', otherwise display a
|
||
message that will say \'Incorrect, the answer is\' and display the real
|
||
answer.
|
||
If they do not select a relevant option on the first menu you should
|
||
display a suitable message."""
|
||
from random import randint
|
||
def menu():
|
||
print(
|
||
"""
|
||
1) Suma
|
||
2) Resta
|
||
""")
|
||
sel = int(input("Ingresa una opción: "))
|
||
match sel:
|
||
case 1:
|
||
num, resp = gen_add()
|
||
check_resp(num, resp)
|
||
case 2:
|
||
num, resp = gen_sub()
|
||
check_resp(num, resp)
|
||
case _:
|
||
print("Debes elegir una opción válida")
|
||
|
||
def gen_add():
|
||
num_1 = randint(25, 50)
|
||
num_2 = randint(25, 50)
|
||
resp = int(input(f"¿Cuanto es {num_1}+{num_2}?: "))
|
||
return num_1+num_2, resp
|
||
|
||
def gen_sub():
|
||
num_1 = randint(25, 50)
|
||
num_2 = randint(1, 25)
|
||
resp = int(input(f"¿Cuanto es {num_1}-{num_2}?: "))
|
||
return num_1-num_2, resp
|
||
|
||
def check_resp(correct, resp):
|
||
if correct == resp:
|
||
print("Correcto!")
|
||
else:
|
||
print(f"Incorrecto, la respuesta es {correct}")
|
||
|
||
menu()
|
||
|
||
def sub_121(self):
|
||
"""Create a program that will allow the user to easily manage a list of
|
||
names. You should display a menu that will allow them to add a name to
|
||
the list, change a name in the list, delete a name from the list or view
|
||
all the names in the list. There should also be a menu option to allow
|
||
the user to end the program. If they select an option that is not
|
||
relevant, then it should display a suitable message. After they have made
|
||
a selection to either add a name, change a name, delete a name or view
|
||
all the names, they should see the menu again without having to restart
|
||
the program. The program should be made as easy to use as possible."""
|
||
lmenu ="""
|
||
1) Agregar nombre
|
||
2) Editar nombre
|
||
3) Borrar nombre
|
||
4) Ver nombres
|
||
0) Salir
|
||
"""
|
||
names = []
|
||
def menu():
|
||
while True:
|
||
print(lmenu)
|
||
sel = int(input("Ingresa una opción: "))
|
||
match sel:
|
||
case 1:
|
||
add_name()
|
||
case 2:
|
||
edit_name()
|
||
case 3:
|
||
del_name()
|
||
case 4:
|
||
show_names()
|
||
case 0:
|
||
break
|
||
case _:
|
||
print("Debes ingresar una opción válida")
|
||
|
||
def add_name():
|
||
name = input("Ingresa el nombre: ").title()
|
||
names.append(name)
|
||
|
||
def edit_name():
|
||
name = input("Ingresa el nombre que quieres editar: ")
|
||
if name in names:
|
||
new_name = input("Ingresa el nuevo nombre: ").title()
|
||
names[names.index(name)] = new_name
|
||
else:
|
||
print(f"{name} no esta en la lista")
|
||
|
||
def del_name():
|
||
name = input("Ingresa el nombre que quieres borrar: ").title()
|
||
if name in names:
|
||
del names[names.index(name)]
|
||
else:
|
||
print(f"{name} no esta en la lista")
|
||
|
||
def show_names():
|
||
print("\nNombres:")
|
||
for name in names:
|
||
print(name)
|
||
|
||
menu()
|
||
|
||
def sub_122(self):
|
||
"""Create the following menu:
|
||
1) Add to file
|
||
2) View all records
|
||
3) Quit program
|
||
Enter the number of your selection:
|
||
If the user selects 1, allow them to add to a file called Salaries.csv
|
||
which will store their name and salary. If they select 2 it should
|
||
display all records in the Salaries.csv file. If they select 3 it should
|
||
stop the program. If they select an incorrect option they should see an
|
||
error message. They should keep returning to the menu until they select
|
||
option 3."""
|
||
from os import getcwd as pwd
|
||
from os.path import isfile
|
||
import csv
|
||
file_path = f"{pwd()}/interm/files/Salaries.csv"
|
||
lmenu ="""
|
||
1) Agregar a archivo
|
||
2) Ver registros
|
||
3) Salir
|
||
"""
|
||
def menu():
|
||
while True:
|
||
print(lmenu)
|
||
sel = int(input("Ingresa una opción: "))
|
||
match sel:
|
||
case 1:
|
||
add_to()
|
||
case 2:
|
||
view_all()
|
||
case 3:
|
||
break
|
||
case _:
|
||
print("Debes ingresar una opción válida")
|
||
|
||
def add_to():
|
||
print("\nAgregando un nuevo registro")
|
||
name = input("Ingresa el nombre: ").title()
|
||
salary = input("Ingresa el salario: ")
|
||
new_data = [name, salary]
|
||
with open(file_path, 'a') as file:
|
||
writer = csv.writer(file)
|
||
writer.writerow(new_data)
|
||
|
||
def view_all():
|
||
if isfile(file_path):
|
||
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(12), line[1].ljust(8))
|
||
else:
|
||
print("El archivo aún no existe, agrega un registro para crearlo")
|
||
|
||
menu()
|
||
|
||
def sub_123(self):
|
||
"""In Python, it is not technically possible to directly delete a record
|
||
from a .csv file. Instead you need to save the file to a temporary list
|
||
in Python, make the changes to the list and then overwrite the original
|
||
file with the temporary list.
|
||
Change the previous program to allow you to do this. Your menu should
|
||
now look like this:
|
||
1) Add to file
|
||
2) View all records
|
||
3) Delete a record
|
||
4) Quit program
|
||
Enter the number of your selection:"""
|
||
from os import getcwd as pwd
|
||
from os.path import isfile
|
||
import csv
|
||
file_path = f"{pwd()}/interm/files/Salaries.csv"
|
||
lmenu ="""
|
||
1) Agregar a archivo
|
||
2) Ver registros
|
||
3) Eliminar un registro
|
||
4) Salir
|
||
"""
|
||
def menu():
|
||
while True:
|
||
print(lmenu)
|
||
sel = int(input("Ingresa una opción: "))
|
||
match sel:
|
||
case 1:
|
||
add_to()
|
||
case 2:
|
||
view_all()
|
||
case 3:
|
||
del_reg()
|
||
case 4:
|
||
break
|
||
case _:
|
||
print("Debes ingresar una opción válida")
|
||
|
||
def add_to():
|
||
print("\nAgregando un nuevo registro")
|
||
name = input("Ingresa el nombre: ").title()
|
||
salary = input("Ingresa el salario: ")
|
||
new_data = [name, salary]
|
||
with open(file_path, 'a') as file:
|
||
writer = csv.writer(file)
|
||
writer.writerow(new_data)
|
||
|
||
def view_all():
|
||
if isfile(file_path):
|
||
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(12), line[1].ljust(8))
|
||
else:
|
||
print("El archivo aún no existe, agrega un registro para crearlo")
|
||
|
||
def del_reg():
|
||
view_all()
|
||
with open(file_path, 'r') as file:
|
||
reader = csv.reader(file)
|
||
contnt = list(reader)
|
||
to_del = int(input("Ingresa el número del registro a borrar: "))
|
||
if to_del < len(contnt):
|
||
del contnt[to_del]
|
||
with open(file_path, 'w') as file:
|
||
writer = csv.writer(file)
|
||
writer.writerows(contnt)
|
||
|
||
menu()
|