Home > front end >  How to retrieve the entire list just by inputting one element from that list?
How to retrieve the entire list just by inputting one element from that list?

Time:12-07

When I type in the Number corresponding to the person, things such as name and birthday should also appear

employInfo=[1,'De Leon','7/19/2001',
           2,'Fabian','6/6/1999'
           3,'Deseo','12/25/1998']
> output should be like:
Enter No.: 3
Employee with this No. is Deseo and his birthday is 12/25/1998.

I really don't know what i'm supposed to be doing here.

CodePudding user response:

A list is the wrong data structure here, and you should be using a dictionary:

d = {1: ['De Leon', '7/19/2001'],
     2: ['Fabian', '6/6/1999'],
     3: ['Deseo', '12/25/1998']}

inp = 3
name = d[inp][0]
birthday = d[inp][1]
output = f"Employee with this No. is {name} and his birthday is {birthday}."
print(output)

This prints:

Employee with this No. is Deseo and his birthday is 12/25/1998.

CodePudding user response:

You should create a class "Employee" and use a dictionary instead of the list:

employInfo =
{
    "1": employee1,
    "2": employee2
}

Then you can access each employee by its id. And you can use class methods to get employee data.

  • Related