# Define an empty List called InfoDb
InfoDb = []
# InfoDB is a data structure with expected Keys and Values
# Append to List a Dictionary of key/values related to a person and cars
InfoDb.append({
"FirstName": "Ethan",
"LastName": "Rahmat",
"DOB": "January 30",
"Residence": "San Diego",
"Email": "ethanr60052@stu.powayusd.com",
"Owns_Cars": ["MacBook-Pro"]
})
# Append to List a 2nd Dictionary of key/values
InfoDb.append({
"FirstName": "Jordan",
"LastName": "Pham",
"DOB": "December 26",
"Residence": "San Diego",
"Email": "jordanp06824@stu.powayusd.com",
"Owns_Cars": ["Lenovo-IdeaPad"]
})
# Append to List a 2nd Dictionary of key/values
InfoDb.append({
"FirstName": "Jake",
"LastName": "Shim",
"DOB": "April 11",
"Residence": "San Diego",
"Email": "jakes10560@stu.powayusd.com",
"Owns_Cars": ["MacBook-Pro"]
})
# Append to List a 2nd Dictionary of key/values
InfoDb.append({
"FirstName": "Dante",
"LastName": "Atanassov",
"DOB": "October 6",
"Residence": "San Diego",
"Email": "dantea15942@stu.powayusd.com",
"Owns_Cars": ["MacBook-Air"]
})
# Print the data structure
print(InfoDb)
[{'FirstName': 'Ethan', 'LastName': 'Rahmat', 'DOB': 'January 30', 'Residence': 'San Diego', 'Email': 'ethanr60052@stu.powayusd.com', 'Owns_Cars': ['MacBook-Pro']}, {'FirstName': 'Jordan', 'LastName': 'Pham', 'DOB': 'December 26', 'Residence': 'San Diego', 'Email': 'jordanp06824@stu.powayusd.com', 'Owns_Cars': ['Lenovo-IdeaPad']}, {'FirstName': 'Jake', 'LastName': 'Shim', 'DOB': 'April 11', 'Residence': 'San Diego', 'Email': 'jakes10560@stu.powayusd.com', 'Owns_Cars': ['MacBook-Pro']}, {'FirstName': 'Dante', 'LastName': 'Atanassov', 'DOB': 'October 6', 'Residence': 'San Diego', 'Email': 'dantea15942@stu.powayusd.com', 'Owns_Cars': ['MacBook-Air']}]
# This jupyter cell has dependencies on one or more cells above
# print function: given a dictionary of InfoDb content
def print_data(d_rec):
print(d_rec["FirstName"], d_rec["LastName"]) # using comma puts space between values
print("\t", "Residence:", d_rec["Residence"]) # \t is a tab indent
print("\t", "Birth Day:", d_rec["DOB"])
print("\t", "Computer Model: ", end="") # end="" make sure no return occurs
print(", ".join(d_rec["Owns_Cars"])) # join allows printing a string list with separator
print()
# for loop algorithm iterates on length of InfoDb
def for_loop():
print("For loop output\n")
for record in InfoDb:
print_data(record) # call to function
for_loop() # call to function
For loop output
Ethan Rahmat
Residence: San Diego
Birth Day: January 30
Computer Model: MacBook-Pro
Jordan Pham
Residence: San Diego
Birth Day: December 26
Computer Model: Lenovo-IdeaPad
Jake Shim
Residence: San Diego
Birth Day: April 11
Computer Model: MacBook-Pro
Dante Atanassov
Residence: San Diego
Birth Day: October 6
Computer Model: MacBook-Air
class Question:
def __init__(self, question, choices, correct_choice):
self.question = question
self.choices = choices
self.correct_choice = correct_choice
def is_correct(self, choice):
return choice == self.correct_choice
def run_quiz(questions):
score = 0
for question in questions:
print(question.question)
for i, choice in enumerate(question.choices, start=1):
print(f"{i}. {choice}")
user_choice = int(input("Enter the number of your choice: "))
if question.is_correct(user_choice):
print("Correct!\n")
score += 1
else:
print(f"Incorrect. The correct answer was {question.correct_choice}: {question.choices[question.correct_choice - 1]}\n")
print(f"You scored {score}/{len(questions)}.")
# Define your quiz questions here
questions = [
Question("What data type is used to organize data patterns?", ["Iteration", "Structure", "Dictionary", "List"], 3),
Question("What computer model does Ethan R have?", ["Lenovo IdeaPad", "MacBook Pro", "MacBook Air", "Chromebook"], 2),
Question("Where is Del Norte High School located?", ["Orange County", "Escondido", "The Bronx", "Del Sur"], 4)
]
# Run the quiz
run_quiz(questions)
What data type is used to organize data patterns?
1. Iteration
2. Structure
3. Dictionary
4. List
Enter the number of your choice: 3
Correct!
What computer model does Ethan R have?
1. Lenovo IdeaPad
2. MacBook Pro
3. MacBook Air
4. Chromebook
Enter the number of your choice: 1
Incorrect. The correct answer was 2: MacBook Pro
Where is Del Norte High School located?
1. Orange County
2. Escondido
3. The Bronx
4. Del Sur
Enter the number of your choice: 4
Correct!
You scored 2/3.