Lists, Dictionaries, Iterations
Adding Records to InfoDb
InfoDb = []
# Append to List a Dictionary of key/values related to a person and cars
InfoDb.append({
"FirstName": "John",
"LastName": "Mortensen",
"DOB": "October 21",
"Residence": "San Diego",
"Email": "jmortensen@powayusd.com",
"Owns_Cars": ["2015-Fusion", "2011-Ranger", "2003-Excursion", "1997-F350", "1969-Cadillac"]
})
# Append to List a 2nd Dictionary of key/values
InfoDb.append({
"FirstName": "Sunny",
"LastName": "Naidu",
"DOB": "August 2",
"Residence": "Temecula",
"Email": "snaidu@powayusd.com",
"Owns_Cars": ["4Runner"]
})
InfoDb.append({
"FirstName": "Martin",
"LastName": "Nguyen",
"DOB": "October 15",
"Residence": "4S Ranch, San Diego",
"Email": "martinsupercell06@gmail.com",
"Owns_Cars": ["None"]})
InfoDb.append({
"FirstName": "Jeffery",
"LastName": "Guo",
"DOB": "July 30",
"Residence": "Carmel Valley, San Diego",
"Email": "jguo45672@gmail.com",
"Owns_Cars": ["BMW"]})
# Print the data structure
print(InfoDb)
Martys_family = ["Barron", "Ben", "Brinley", "Martin", "Chuck", "Minh"]
# reverse the order of list elements
Martys_family.reverse()
print('Reversed List:', Martys_family)
For Loop Demonstration
def print_data(info):
print(info["FirstName"], info["LastName"]) # using comma puts space between values
print("\t", "Residence:", info["Residence"]) # \t is a tab indent
print("\t", "Birth Day:", info["DOB"])
print("\t", "Cars: ", end="") # end="" make sure no return occurs
print(",".join(info["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 info in InfoDb:
print_data(info)
for_loop()
While Loop Demonstration
def while_loop():
print("While loop output\n")
print("This loop continues to print until the length of the InfoDb is met\n")
i = 0
while i < len(InfoDb):
info = InfoDb[i]
print_data(info)
i += 1
return
while_loop()
Recursion loop demonstration
def MartysRecursive(i):
if i < len(InfoDb):
info = InfoDb[i]
print_data(info)
MartysRecursive(i + 1)
return
print("Recursive loop output\n")
print("This is an alternate version of the while loop that calls itself until the conditions of the loop are met.\n")
MartysRecursive(0)
Python Quiz utilizing dictionaries and lists
questions = 5 #number of quiz questions
correct = 0 #initialize number of correct answers as 0
print("This is a 5 question NBA Quiz :)")
def question_and_answer(prompt, answer):
print("Question: " + prompt) #asks user a question
message = input() #takes user's input as variable msg
print("Answer: " + message) #print user's input as Answer
if answer == message:
print("Correct Answer")
global correct #allows to modify variable outside of current scope
correct += 1 #add 1 to correct count
else:
print ("Incorrect Answer")
return message
question_1 = question_and_answer("How many championships have the Lakers won?", "17")
question_2 = question_and_answer("How many championships have the Celtics won?", "17")
question_3 = question_and_answer("Whos the best shooter in the NBA?", "Stephen Curry")
question_4 = question_and_answer("What is the most points score in a game?", "100")
question_5 = question_and_answer("Who is the leading scorer in all time points?", "Kareem Abdul Jabbar")
if correct < 3:
print(f'You scored {correct} correct answers out of 5, you know nothing!')
elif correct < 5:
print(f'You scored {correct} correct answers out of 5, your somwhat of a fan!')
else:
print(f'You scored {correct} correct answers out of 5, you really are a true NBA fan!')
Quiz = {
"Q_1": question_1,
"Q_2": question_2,
"Q_3": question_3,
"Q_4": question_4,
"Q_5": question_5
}
print("A record of your quiz:", Quiz)