Skip to content

Instantly share code, notes, and snippets.

@davegotz
Created March 19, 2019 14:49
Show Gist options
  • Save davegotz/89b354ab5fb9f30be4e9f2545b732b9f to your computer and use it in GitHub Desktop.
Save davegotz/89b354ab5fb9f30be4e9f2545b732b9f to your computer and use it in GitHub Desktop.
Student directory with dictionaries
# Create a student directory by allowing the user to enter all data.
def create_student_directory():
all_students_by_onyen = {}
all_students_by_email = {}
all_students_by_name = {}
# Get as many students as the user wants to enter,
# storing each in “students” onyen as key.
stud_name = input('Enter student name (enter blank line to quit): ')
while stud_name != '':
one_student = {'name': stud_name}
onyen = input('Enter onyen: ')
one_student['onyen'] = onyen
email = input('Enter email: ')
one_student['email'] = email
# Store this “one student” in the all students dictionary.
if onyen not in all_students_by_onyen:
all_students_by_onyen[onyen] = one_student
else:
print("Repeat onyen. Not adding to directory.")
all_students_by_email[email] = one_student
if stud_name in all_students_by_name:
# Watch out
all_students_by_name[stud_name].append(one_student)
else:
all_students_by_name[stud_name] = [one_student]
# Prep for next iteration
stud_name = input('Enter student name (enter blank line to quit): ')
return all_students_by_onyen, all_students_by_email, all_students_by_name
# Create a student directory and test it by looking up a value.
def main():
directory = create_student_directory()
# Look for a student with onyen of ‘gotz’
if 'gotz' in directory:
print(directory['gotz']['name'])
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment