2

In this code, I wanted to identify a member with his ID. If the ID exist in the dictionary, I would like to print the fullname of this member

iin = input('Enter your ID :')
 dict = {'id' : ['aa', 'bb', 'cc'],
        'Firstname' : ['Mark', 'Jamal', 'Van'],
        'Lastname' : ['Roch', 'borh', 'nilsa']}
 for d in dict:
    if inn in d:
       print('Hello,', dict[inn])
    else :
       print('Sorry, you are not a member')

The desired result

Enter your ID : aa
Hello, Mark Roch

Thank you for the help

Dinesh Pundkar
  • 4,160
  • 1
  • 23
  • 37
K. ossama
  • 403
  • 1
  • 4
  • 15
  • 1
    Get the index of your ID from the first dictionary element (list) and use it to access the other lists: `dict["Firstname"][index]` – handle Aug 29 '16 at 16:10
  • But your code won't work as `d` only gets you the dictionary keys "id", "Firstname", etc. You want to test ìn dict[d]` to get to the lists. – handle Aug 29 '16 at 16:12
  • Exactly that, but i'm stuck can you give me the good syntax! thanks – K. ossama Aug 29 '16 at 16:17
  • @K.ossama: I have added few explainations with my answer. Hope those may help, because there are basic programming issues with your code – Moinuddin Quadri Aug 29 '16 at 16:37

4 Answers4

3

There's no need to loop over all the items in the dictionary; it would be silly to look for an id in the Firstname and Lastname fields. (And this could produce incorrect results if the entered id happens to be a substring of someone's name.)

What you want is to check if the given id is present in the id list, and if so, use that list position to print the corresponding first and last names:

if iin in dict['id']:
   index = dict['id'].index(iin)
   print('Hello, %s %s' % (dict['Firstname'][index], dict['Lastname'][index]))
else :
   print('Sorry, you are not a member')

Some suggestions:

It might be easier if you arranged your data as a list of dicts each containing a single member, like so:

members = [
    {'id': 'aa', 'Firstname': 'Mark',  'Lastname': 'Roch'},
    {'id': 'bb', 'Firstname': 'Jamal', 'Lastname': 'borh'},
    {'id': 'cc', 'Firstname': 'Van',   'Lastname': 'nilsa'},
]

Don't name your dictionary dict, as that conflicts with a built-in function name.

John Gordon
  • 29,573
  • 7
  • 33
  • 58
3

Please check the below code with comments inline.

iin = input('Enter your ID :')
d = {'id' : ['aa', 'bb', 'cc'],
        'Firstname' : ['Mark', 'Jamal', 'Van'],
        'Lastname' : ['Roch', 'borh', 'nilsa']}
#Get list of IDs    
id_list = d['id']

#Check input in list
if iin in id_list:
    #Get index of input from ID list
    index = id_list.index(iin)
    #Get rest of info
    fname = d['Firstname'][index]
    lname = d['Lastname'][index]
    msg = "Hello, " + fname + " " + lname
    print msg
else:
    print 'Sorry, you are not a member'

Output :

C:\Users\dinesh_pundkar\Desktop>python b.py
Enter your ID :"aa"
Hello, Mark Roch

C:\Users\dinesh_pundkar\Desktop>python b.py
Enter your ID :"asd"
Sorry, you are not a member

C:\Users\dinesh_pundkar\Desktop>
Dinesh Pundkar
  • 4,160
  • 1
  • 23
  • 37
0

The working version of the code should be:

my_dict = {'id' : ['aa', 'bb', 'cc'],
           'Firstname' : ['Mark', 'Jamal', 'Van'],
               'Lastname' : ['Roch', 'borh', 'nilsa']}
iin = input('Enter your ID :')
# Enter your ID :"bb"
try:
    my_id = my_dict['id'].index(iin)
    print my_dict['Firstname'][my_id], ', ', my_dict['Lastname'][my_id]
except ValueError:
    print 'Sorry, you are not a member'    
# Jamal ,  borh

Exlaination/Issues with your code:

  1. for d in dict means for d in dict.keys() which will return ['id', 'FirstName', 'LastName'. In order to iterate over id, you should be doing for d in dict['id']. In fact, iterating over the list itself is not required. To get index of element in ;list, you may simply use list.index(element) function
  2. dict is the built-in class in python. You should never use the keywords
  3. You dictionary structure is itself not correct. By definition, dict means collections of objects and object means similar entities. In this case, Person. Since id is supposed by unique, in am making nested dict with key as id (you may also use list of dict):

    {'aa': {'Firstname' : 'Mark',
            'Lastname' : 'Roch'},
     'bb': {'Firstname' : 'Jamal',
            'Lastname' : 'borh'},
     'cc': {'Firstname' : 'Van',
            'Lastname' : 'nilsa'},
    }
    
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
0

Building on previous posts

iin = input('Enter your ID :')
dict = {'id' : ['aa', 'bb', 'cc'],
        'Firstname' : ['Mark', 'Jamal', 'Van'],
        'Lastname' : ['Roch', 'borh', 'nilsa']}
try:
    i = dict['id'].index(iin)
    print('Hello {0} {1}'.format(dict['Firstname'][i], dict['Lastname'][i])
except ValueError:
    print('Sorry, you are not a member')

NOTE: you cannot name a dictionary dict because it conflicts with a keyword.

Community
  • 1
  • 1
Terrence Brannon
  • 4,760
  • 7
  • 42
  • 61