forked from mouredev/roadmap-retos-programacion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouredev.py
100 lines (89 loc) · 2.82 KB
/
mouredev.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import os
"""
Ejercicio
"""
file_name = "mouredev.txt"
with open(file_name, "w") as file:
file.write("Brais Moure\n")
file.write("36\n")
file.write("Python")
with open(file_name, "r") as file:
print(file.read())
os.remove(file_name)
"""
Extra
"""
file_name = "mouredev_shop.txt"
open(file_name, "a")
while True:
print("1. Añadir producto")
print("2. Consultar producto")
print("3. Actualizar producto")
print("4. Borrar producto")
print("5. Mostrar productos")
print("6. Calcular venta total")
print("7. Calcular venta por producto")
print("8. Salir")
option = input("Selecciona una opción: ")
if option == "1":
name = input("Nombre: ")
quantity = input("Cantidad: ")
price = input("Precio: ")
with open(file_name, "a") as file:
file.write(f"{name}, {quantity}, {price}\n")
elif option == "2":
name = input("Nombre: ")
with open(file_name, "r") as file:
for line in file.readlines():
if line.split(", ")[0] == name:
print(line)
break
elif option == "3":
name = input("Nombre: ")
quantity = input("Cantidad: ")
price = input("Precio: ")
with open(file_name, "r") as file:
lines = file.readlines()
with open(file_name, "w") as file:
for line in lines:
if line.split(", ")[0] == name:
file.write(f"{name}, {quantity}, {price}\n")
else:
file.write(line)
elif option == "4":
name = input("Nombre: ")
with open(file_name, "r") as file:
lines = file.readlines()
with open(file_name, "w") as file:
for line in lines:
if line.split(", ")[0] != name:
file.write(line)
elif option == "5":
with open(file_name, "r") as file:
print(file.read())
elif option == "6":
total = 0
with open(file_name, "r") as file:
for line in file.readlines():
components = line.split(", ")
quantity = int(components[1])
price = float(components[2])
total += quantity * price
print(total)
elif option == "7":
name = input("Nombre: ")
total = 0
with open(file_name, "r") as file:
for line in file.readlines():
components = line.split(", ")
if components[0] == name:
quantity = int(components[1])
price = float(components[2])
total += quantity * price
break
print(total)
elif option == "8":
os.remove(file_name)
break
else:
print("Selecciona una de las opciones disponibles.")