2

Case: I need to get user input of employee ID using input() to view employee details contained in a dictionary.

Problem: But the output of input() is a string. How can I relate an employee ID to lookup for their information already contained in a dictionary. I try to use LIST but not successful.

Code:

Given list of Dictionary (employee ID number) containing employee detail

ID001 ={'Name':'John Clause', 'Age':'21' , 'Gender':'Male'}    
ID002 ={'Name':'Greg Pyuse' , 'Age':'21' , 'Gender':'Male'}    

Populate List with dictionary"

List = [ID001,ID002]

Require a user input() to ask for Dictionary i.e. 'ID001' or 'ID002'

ID = input()

I want to display list index corresponding to dictionary fron user input

ID_Index=List.index(ID)

If I run:

ValueError Traceback (most recent call last) in 3 4 #Get the list index of ID Number ----> 5 ID_Index=List.index(ID) 6 print(type(ID)) 7 #Display content of list with particular index

ValueError: 'ID002' is not in list

webuser
  • 123
  • 2
  • 5
  • 1
    Possible duplicate of [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – Aran-Fey Oct 30 '18 at 22:45

2 Answers2

1

If you want to be able to look up an entry by a key, you should use a dict instead:

Dict = {
    'ID001': {'Name':'John Clause', 'Age':'21' , 'Gender':'Male'},
    'ID002': {'Name':'Greg Pyuse' , 'Age':'21' , 'Gender':'Male'}
}

so that you can use Dict[ID] to obtain the dict {'Name':'John Clause', 'Age':'21' , 'Gender':'Male'} if ID is ID001.

blhsing
  • 91,368
  • 6
  • 71
  • 106
  • thanks, the problem can be done by dictionary. I found out it can be combined with a LIST. Its more of programming approach than a problem. I will show my resolution – webuser Nov 01 '18 at 21:43
0

I Elaborate my resolution base on answer by bhlsing for future viewers

Define Dictionary

info_Dict={}
Student={}

Create Dictionary with three keys and values

for any_variable in range(0,3):
    id=input('Enter I.D. ') 

#Populate dictionary (info_Dict) with student's information using input()
    info_Dict['Name']=input("Name: ") 
    info_Dict['Age'] =input("Age: ")
    info_Dict['Course']=input("Course: ")

#Add (info_Dict) to another dictionary (Student) whose keys are student's I.D.
    Student['ID00'+id]=(info_Dict)

#Empty Dictionary of students (info_Dict) so we can add new entry 
#on next loop - it act as temporary variable.
    info_Dict={}  

print(Student)

If I ran, It will ask in 3 loops for inputs, I entered as follows:

Enter I.D. 1
Name: MATT MONROE 
Age: 88
Course: PYTHON 
Enter I.D. 2
Name: JAMES COOPER 
Age: 68
Course: JAVA 
Enter I.D. 3
Name: CATE HOLMES 
Age: 52
Course: AUTOMOTIVE 

{'ID001': {'Name': 'MATT MONROE ', 'Age': '88', 'Course': 'PYTHON '},
 'ID002': {'Name': 'JAMES COOPER ', 'Age': '68', 'Course': 'JAVA '},
 'ID003': {'Name': 'CATE HOLMES ', 'Age': '52', 'Course': 'AUTOMOTIVE '}}

I can now edit based on ID number

Student['ID002']={'NAME':input('NAME'),'Age':input('Age'), 'Course':input('Course')}
Student

If i run it will ask input, it will then override the previous data of ID002

NAMEANNE CURTIS 
Age32
CourseFINE ARTS 

{'ID001': {'Name': 'MATT MONROE ', 'Age': '88', 'Course': 'PYTHON '},
 'ID002': {'NAME': 'ANNE CURTIS ', 'Age': '32', 'Course': 'FINE ARTS '},
 'ID003': {'Name': 'CATE HOLMES ', 'Age': '52', 'Course': 'AUTOMOTIVE '}}
webuser
  • 123
  • 2
  • 5