I am doing a job for the university, they asked me to make a code with classes where I can enter the name, hours worked and the salary of N workers, then calculate a bonus depending on the hours worked and print the data of the workers right at their bonus, I have a class Trabajador
where I have the class variables "name, hours and salary" with the __init__
defined instance variables, then I have another class Main
where I have the data entry method, in this method I define a list where the data is They would be saving lista_trab
, all this in a file, then I have another file Main1
where I import everything from the file with the classes, this fileMain1
It is the one with the Menu where the options are entered, 1 to enter data, in this option I call the method of entering the class Main
, 2 is to print the list with the data of the workers, and this is where I have my problem , in the file Main1
I made a list lista_trabajadores = []
to save the data of the list lista_trab
and then print it, that is the operation that I don't know how to do, since every time I print the list lista_trabajadores
it is printed empty since that is how I defined it in the file Main1
, I don't know where and how to make the link of the 2 lists so that when you print it lista_trabajadores
you have the data of thelista_trab
... I was told that the data model of the code was wrong but that is not my concern at the moment since I have little time left to deliver the code, if there is a better way to do it without having to create another list in the file Main1
and simply call the lista_trab
, better yet, but I don't know how to do that either, also in the file with the classes in the data entry def, I put a print after doing the .append
with the data to the lista_trab
, but that print is even running ...
As you may have noticed, I am new to programming and if you could help me with the code so that it works correctly, I would really appreciate it. They have given me explanations but I still don't quite understand how Python works. I leave you my code
File with classes
class Trabajador:
nombre=""
horas = 0
sueldo = 0
def __init__(self,nombre,horas,sueldo):
self.nombre=nombre
self.horas=horas
self.sueldo=sueldo
pass
class Main:
def ingreso_trabajador(lista_trab,nombre,horas,sueldo):
Trabajador.nombre=input("Ingrese el nombre del trabajador: ")
while True:
try:
Trabajador.horas = float(input("Ingrese las horas trabajadas: "))
break
except ValueError:
print("Ingrese un valor numerico, intentelo nuevamente")
while True:
try:
Trabajador.sueldo = int(input("Ingrese el sueldo del trabajador: "))
if (Trabajador.horas < 30):
bono=(Trabajador.sueldo * 15 / 100)
print(bono)
break
elif (Trabajador.horas >= 30 and Trabajador.horas < 40):
bono=(Trabajador.sueldo * 25 / 100)
print(bono)
break
elif (Trabajador.horas >= 40):
bono=(Trabajador.sueldo * 35 / 100)
print (bono)
break
except ValueError:
print("Ingrese un valor numerico entero, intentelo nuevamente")
T=Main(Trabajador.nombre, Trabajador.horas, Trabajador.sueldo)
lista_trab.append(T)
opc=input("Desea ingresar otro trabajador? si/no")
if opc=="no":
print(lista_trab)
break
program file
from Trabajador1 import *
print("----------------------------------------------")
print("MENU PRINCIPAL")
print("1- Ingresar Trabajador")
print("2- Mostrar los datos de los trabajadores")
print("0- Salir")
lista_trabajadores=[]
while True:
try:
opc = int(input("Ingrese su opcion: "))
break
except ValueError:
print("Debe ingresar una opcion valida, intentelo nuevamente")
while opc!=0:
if opc == 1:
while True:
Main.ingreso_trabajador(lista_trabajadores)
opc=input("Desea ingresar otro trabajador? si/no : ")
if opc == "no":
break
if opc==2:
print(lista_trabajadores) #aca solamente estoy dejando el print para ver si los
#datos se estan guardando
print("----------------------------------------------")
print("MENU PRINCIPAL")
print("1- Ingresar Trabajador")
print("2- Mostrar los datos de los trabajadores")
print("0- Salir")
opc = int(input(">"))
There are so many errors and misconceptions that I don't know where to start. Use this code to illustrate the goal you aspire to. His study will be of benefit to you.
show