1

My program begins with a list of names. eg: ['Bob','John','Mike']' This list is then shuffled into a random order. eg: ['Mike','Bob','John'] A name then gets taken from the list. eg: ['Mike'] leaving ['Bob','John']

I would then like to associate this name with a dictionary of the same name. eg: 'Mike' = {'Surname' : 'Jones', 'Age': 55, 'Staff ID': 101}

Then be able to call and print a particular Key : Value of the chosen name. eg: print(Mike[Age])

(my code at present is similar to this example)

list_of_names = ['Bob','John','Mary','Joan','Mike']
chosen_name = list_of_names.pop(0)
print("person chosen: ", (chosen_name))

# Dictionaries are pre-formatted waiting to be paired with their listed name
'Bob' = {'Surname' : 'Kelly', 'Age': 49, 'Staff ID': 86},
'John' = {'Surname' : 'Hogan', 'Age': 57, 'Staff ID': 22},
'Mike' = {'Surname' : 'Jones', 'Age': 55, 'Staff ID': 101},

# Prints the randomly chosen name and the dictionary associated with it.
print(chosen_name) 

# Prints a Value for a particular key of that chosen name
print(chosen_name[Age]) 

I would greatly appreciate any advice or even alternative methods of achieving this. Many thanks.

  • Show what you've tried, by clicking the "edit" button and adding it to your query. – hd1 Mar 02 '19 at 23:47

1 Answers1

1

I'd say it's probably easiest to just have another dictionary in your code. For example:

list_of_names = ['Bob','John','Mary','Joan','Mike']
chosen_name = list_of_names.pop(0)
print("person chosen: ", (chosen_name))

# Dictionaries are pre-formatted waiting to be paired with their listed name
person_info = {}
person_info['Bob']  = {'Surname' : 'Kelly', 'Age': 49, 'Staff ID': 86}
person_info['John'] = {'Surname' : 'Hogan', 'Age': 57, 'Staff ID': 22}
person_info['Mike'] = {'Surname' : 'Jones', 'Age': 55, 'Staff ID': 101}

# Prints the randomly chosen name and the dictionary associated with it.
print(person_info[chosen_name])

# Prints a Value for a particular key of that chosen name
print(person_info[chosen_name]['Age']) 
Mr. Snrub
  • 322
  • 2
  • 13