Info Data Base
List Dictionary Iteration using Python
The database InfoDb has information on two schools. Each including their name, principal, year the school was built, school level, number of students, number of teachers, zip code, and the school colors. Using the school name, this information will be found through through the dictionary.
InfoDb = []
# Append to List a Dictionary of key/value related to the school and information
InfoDb.append({
"SchoolName": "Del Norte High School",
"Principal": "Bryan Schultz",
"Founded": "2009",
"SchoolLevel": "Highschool",
"StudentNumber": "2585",
"TeacherNumber": "92",
"Zipcode": "92127",
"SchoolColors": ["Navy Blue, Forest Green, Vegas Gold"]
})
# Append to List a 2nd Dictionary with key/values
InfoDb.append({
"SchoolName": "Westview High School",
"Principal": "Ernie Remillard",
"Founded": "2002",
"SchoolLevel": "Highschool",
"StudentNumber": "2376",
"TeacherNumber": "85",
"Zipcode": "92129",
"SchoolColors": ["Black, White, Vegas Gold"]
})
# Print Data Structure
print(InfoDb)
In the code above, both schools information was saved in the dictionary.
def print_data(d_school):
print(d_school["SchoolName"])
print("\t", "Principal:", d_school["Principal"])
print("\t", "Founded:", d_school["Founded"])
print("\t", "School Type:", d_school["SchoolLevel"])
print("\t", "Number of Students:", d_school["StudentNumber"])
print("\t", "Number of Teachers:", d_school["TeacherNumber"])
print("\t", "ZipCode:", d_school["Zipcode"])
print("\t", "School Colors:", d_school["SchoolColors"])
# for loop algorithm iterates on length of InfoDb
def for_loop():
for record in InfoDb:
print_data(record)
for_loop()
In the code above, the for loop was able to iterate over the InfoDb to find the information of the school and print the information.
def while_loop():
i = 0
while i < len(InfoDb):
record = InfoDb[i]
print_data(record)
i += 1
return
while_loop()
The while loop above printed the school's information as long as the variable being used is less than the length of the InfoDb.
def recursive_loop(i):
if i < len(InfoDb):
record = InfoDb[i]
print_data(record)
recursive_loop(i + 1)
return
recursive_loop(0)
The code above was able to print the school information by using a recursive loop, which calls itself repeatedly.
input_schoolname = input("Enter which school you would like to search for:")
input_information = input("Enter what you are searching for: ex. Principal, SchoolName, Founded, SchoolLevel, StudentNumber, TeacherNumber, Zipcode, or SchoolColors")
# define function to print records
def search_school():
for info in InfoDb:
found_school = 0
found_info = 0
found_key = ""
for key in info:
if (found_school == 0):
if info[key] == input_schoolname:
found_school = 1
# print("School is found" + str(found_school) + "\n")
if (found_info == 0):
if key == input_information:
found_key = key
found_info = 1
# print("key is " + key + "\n")
if (found_school == 1 and found_info == 1):
print("School Name: " + input_schoolname)
print("Information: " + found_key + ": " )
print(info[found_key])
return
if (found_school == 0 or found_info == 0):
print("Information not found.")
search_school()
Using a for loop the code was able to take information from the dictionary and compare it with the user input. By doing so, the user can choose which piece of information it would like to view.
input_schoolname = input("Enter which school you would like to search for:")
input_information = input("Enter what you are searching for: ex. Principal, SchoolName, Founded, SchoolLevel, StudentNumber, TeacherNumber, Zipcode, or SchoolColors")
# define function to print records
def find_school():
found_school = False
found_info = False
i = 0
while (found_school == False or found_info == False):
found_key = ""
info = InfoDb[i]
i += 1
for key in info:
if (found_school == 0):
if info[key] == input_schoolname:
found_school = True
# print("School is found" + str(found_school) + "\n")
if (found_info == 0):
if key == input_information:
found_key = key
found_info = True
# print("key is " + key + "\n")
if (found_school == True and found_info == True):
print("School Name: " + input_schoolname)
print("Information: " + found_key + ": " )
print(info[found_key])
if (found_school == False or found_info == False):
print("Information not found.")
find_school()
Doing something similar to the for loop, a while loop was used. The while loop was able to find the information the user wanted as long as the conditions were true.