How can I make it so you input a "Worker code" and get the worker details same as when you "print(Worker_36.details)"? – Python

Question:

I am new to python and just playing around please help!

Worker_31 = Worker('David', 'Williamson',31 , 92500, 5, 37)
Worker_32 = Worker('Frank', 'Murphy',32 , 58500, 6, 27)
Worker_33 = Worker('Josephine', 'Dover',33 , 69500, 2, 30)
Worker_34 = Worker('Chester', 'Cohen',34 , 88500, 3, 52)
Worker_35 = Worker('Saba', "Brenland",35 , 96500, 4, 35)
Worker_36 = Worker('Tommy-Lee', 'Briggs',36 , 98500, 3, 57)
Worker_37 = Worker('Li', 'Hu-Tao',37 , 55000, 3, 22)
Worker_38 = Worker('Qin', 'Shi-Huang',38 ,14 ,1500000 , 34)
Worker_39 = Worker('Maximillian', 'Mendoza',39 , 200000, 13, 33)
Worker_40 = Worker('Sarah', 'Patel',40 , 86500 , 8, 29)
Worker_41 = Worker('Sumaiya', 'Johns',41 ,77900 , 10, 32)

So you see I was able to make the workers

def details(self):
    return '{}' '{}' '{}' '{}' '{}' '{}' '{}' '{}' '{}' '{}' '{}' '{}' '{}' '{}' '{}' .format("Your worker ", self.fullname(), " (Worker number ", self.number, ") ", "is paid a yearly salary of ", "$"+str(self.pay), " Dollars. ", "This worker has been with your company for ", self.time_with, " years, unfortunately however, they are due to retire in ", self.time_left, " years (aged 65).", " In the mean time however you can contact this worker with the email address ", self.email)

And able to print(Worker_36.details) for example and it works…

print("You have 50 workers what worker would you like to check the details of?")
Worker_Number_Check = input("Please input there worker number ")

If the user inputs 36 for example I want it to return the equavilent of the "print(Worker_36.details)"

I don’t want to have to do a long else if for every single possible input number with a worker who has that as there "Worker number", please help?

Asked By: The Stupid Person

||

Answers:

Instead of declaring many separate Worker variables, make a list of them:

workers = [
    Worker(...),
    Worker(...),
    Worker(...),
    Worker(...),
]

And then you can refer to workers[36].

Answered By: John Gordon
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.