Python lists

Add Values to python DB


# 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": "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"]
})

# Append to List a 2nd Dictionary of key/values
InfoDb.append({
    "FirstName": "Shane",
    "LastName": "Lopez",
    "DOB": "February 27",
    "Residence": "San Diego",
    "Email": "???@powayusd.com",
    "Owns_Cars": ["2021-Insight"]
})

InfoDb.append({
    "FirstName": "Saathvik",
    "LastName": "Gampa",
    "DOB": "March 08",
    "Residence": "San Diego",
    "Email": "???@powayusd.com",
    "Owns_Cars": ["Honda-CRV"]
})

InfoDb.append({
    "FirstName": "Sri",
    "LastName": "Vaidya",
    "DOB": "March 08 2008",
    "Residence": "San Diego",
    "Email": ".....@gmail.com",
    "Owns_Cars": ["Acura-RDX", "Honda-CRV"]

})

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", "DOB:", 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()


def recursive_loop(i):
    if i < len(InfoDb):
        record = InfoDb[i]
        print_data(record)
        recursive_loop(i + 1)
    return
    
print("Recursive loop output\n")
recursive_loop(0)
Recursive loop output

John Mortensen
	 Residence: San Diego
	 DOB: October 21
	 Cars: 2015-Fusion, 2011-Ranger, 2003-Excursion, 1997-F350, 1969-Cadillac

Sunny Naidu
	 Residence: Temecula
	 DOB: August 2
	 Cars: 4Runner

Shane Lopez
	 Residence: San Diego
	 DOB: February 27
	 Cars: 2021-Insight

Saathvik Gampa
	 Residence: San Diego
	 DOB: March 08
	 Cars: Honda-CRV

Sri Vaidya
	 Residence: San Diego
	 DOB: March 08 2008
	 Cars: Acura-RDX, Honda-CRV

4th Way to For Loop

# 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": "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"]
})

# Append to List a 2nd Dictionary of key/values
InfoDb.append({
    "FirstName": "Shane",
    "LastName": "Lopez",
    "DOB": "February 27",
    "Residence": "San Diego",
    "Email": "???@powayusd.com",
    "Owns_Cars": ["2021-Insight"]
})

InfoDb.append({
    "FirstName": "Saathvik",
    "LastName": "Gampa",
    "DOB": "March 08",
    "Residence": "San Diego",
    "Email": "???@powayusd.com",
    "Owns_Cars": ["Honda-CRV"]
})

InfoDb.append({
    "FirstName": "Sri",
    "LastName": "Vaidya",
    "DOB": "March 08 2008",
    "Residence": "San Diego",
    "Email": ".....@gmail.com",
    "Owns_Cars": ["Acura-RDX", "Honda-CRV"]

})

for index, dictionary in enumerate(InfoDb):
    print(f"Dictionary {index}:")
    for key, value in dictionary.items():
        print(key, value)
    print()  # For spacing between dictionaries


Dictionary 0:
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']

Dictionary 1:
FirstName Sunny
LastName Naidu
DOB August 2
Residence Temecula
Email snaidu@powayusd.com
Owns_Cars ['4Runner']

Dictionary 2:
FirstName Shane
LastName Lopez
DOB February 27
Residence San Diego
Email ???@powayusd.com
Owns_Cars ['2021-Insight']

Dictionary 3:
FirstName Saathvik
LastName Gampa
DOB March 08
Residence San Diego
Email ???@powayusd.com
Owns_Cars ['Honda-CRV']

Dictionary 4:
FirstName Sri
LastName Vaidya
DOB March 08 2008
Residence San Diego
Email .....@gmail.com
Owns_Cars ['Acura-RDX', 'Honda-CRV']

Quiz in python storing in dictiories

def start_quiz(questions):
    score = 0

    for question, answer in questions.items():
        user_answer = input(question + " ")
        if user_answer.lower() == answer.lower():
            print("Correct!\n")
            score += 1
        else:
            print(f"Wrong! The correct answer is {answer}\n")

    print(f"You scored {score} out of {len(questions)}!")

# Questions and answers about Disney movies
quiz = {
    "Who is the main villain in 'The Little Mermaid'?": "Ursula",
    "Which Disney movie features a talking teapot and her son, a cup?": "Beauty and the Beast",
    "What is the name of Simba's father in 'The Lion King'?": "Mufasa",
    "In 'Frozen', who is Anna's sister?": "Elsa",
    "Which Disney movie is set in the city of Agrabah?": "Aladdin"
}

start_quiz(quiz)

Correct!

Correct!

Correct!

Correct!

Correct!

You scored 5 out of 5!