Python Hacks
Python Hacks
- 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": "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": "Prasith",
    "LastName": "Chilla",
    "DOB": "January 9",
    "Residence": "San Diego",
    "Email": "prasithchilla@gmail.com",
    "Owns_Cars": ["none"]
})
# Print the data structure
print(InfoDb)
x = len(InfoDb)
print(x)
- Index with For loop:
 
x = len(InfoDb)
print(x)
i = 0
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", "Cars: ", end="")  # end="" make sure no return occurs
    print(", ".join(d_rec["Owns_Cars"]))  # join allows printing a string list with separator
    print()
for i in range (x):
    record = InfoDb[i]
    print_data(record)
    i += 1 
QuizDb = []
QuizDb.append({
    "Q1": "Who won the 2021-2022 NBA Championship?",
    "A1":"Warriors",
    "Q2": "Which NBA franchise has the most MVP winners?",
    "A2": "Celtics",
    "Q3": "Who won the 2021-2022 NBA MVP?",
    "A3": "Nikola Jokic",
    "Q4": "Which NBA team(s) has the most rings?",
    "A4": "Celtics"
   
})
def quiz():
    s = 1
    points = 0 
    while s <= 4:
        answer = input(QuizDb[0]["Q"+str(s)])
        if answer == QuizDb[0]["A"+str(s)]:
            print("Correct")
            points += 1 
        else: 
            print("Incorrect")
        s +=1
    print("You got", 100*(points/4), "%")
quiz()