- List and Dictionary purpose
- Using this information, I will make a quiz that stores in a List of Dictionaries and asking questions about you!
List and Dictionary purpose
- List is used to collect many
- Dictionary is used to define data patterns.
- Iteration is often used to process through lists.
- A 'list' data type has the method '.append(expression)' that allows you to add to the list
- The function '.append(expression)' of a 'list' data type lets you to add to the list.
So I will use the InfoDb command to sort the important dates for class.
InfoDb = []
# Append to List a Dictionary
InfoDb.append({
"ClassName": "AP Chemistry",
"Period": "2",
"TypeOfTest": "FRQ",
"Date": "9/7/22",
"StudyMaterial": "AP Style Test Problems",
"TeacherEmail": "kozuna@powayusd.com",
})
# Append to List a 2nd Dictionary of key/values
InfoDb.append({
"ClassName": "AP Calculus",
"Period": "3",
"TypeOfTest": "MC and FRQ",
"Date": "9/9/22",
"StudyMaterial": "Review Homework",
"TeacherEmail": "jbuehler@powayusd.com",
})
# Append to List a 3rd Dictionary of key/values
InfoDb.append({
"ClassName": "AP Studio Art",
"Period": "1",
"TypeOfTest": "Submission of Art",
"Date": "9/1/22",
"StudyMaterial": "Finish First Art Piece",
"TeacherEmail": "pcoleman@powayusd.com",
})
# Append to List a 4th Dictionary of key/values
InfoDb.append({
"ClassName": "AP CompSci",
"Period": "4",
"TypeOfTest": "Submission of Hacks",
"Date": "9/5/22",
"StudyMaterial": "Finish Hacks at bottom page",
"TeacherEmail": "syeung@powayusd.com",
})
# Print the data structure
print(InfoDb)
This is the for loop, which is a loop that can be used for lists. I personally find this loop the easiest to use.
def print_data(d_rec):
print(d_rec["ClassName"], "Period:", d_rec["Period"]) # using comma puts space between values
print("\t", "Type Of Test:", d_rec["TypeOfTest"]) # \t is a tab indent
print("\t", "Date:", d_rec["Date"])
print("\t", "Study Material:", d_rec["StudyMaterial"])
print("\t", "Teacher Email:", d_rec["TeacherEmail"])
print()
# for loop iterates on length of InfoDb
def for_loop():
print("For loop output\n")
for record in InfoDb:
print_data(record)
for_loop()
InfoDb = []
print("Hi! This computer wants to know more stuff about you!")
rsp = input ("Are you ready?")
if rsp == "yes":
print ("Thanks!\n")
if rsp == "no":
print ("Oh no.\n")
print ("What is your name?")
name = input ('What is your name?\n')
print ("Hello " + name + "! How old are you?")
age = input ('How old are you?\n')
print ("Wow you're " + age + "! What grade are you in?")
grade = input ('What grade are you in\n')
print (grade + "th grade! What is your favorite class right now?")
favoriteClass = input ('What is your favorite class right now\n')
print (favoriteClass +"! That is my favorite, too! Thanks for taking this quiz, have a nice day!")
InfoDb.append({
"Name" : name,
"Age" : age,
"Grade" : grade,
"Favorite Class" : favoriteClass,
})
def print_data(d_rec):
print("Name:", d_rec["Name"])
print("Age:", d_rec["Age"])
print("Grade:", d_rec["Grade"])
print("Favorite Class:", d_rec["Favorite Class"])
print()
# for loop iterates on length of InfoDb
def for_loop():
print("For loop output\n")
for record in InfoDb:
print_data(record)
for_loop()