Dictionaries and Lists
- Shoe Dictionary
- Adding Records to the InfoDb
- Using Index in For Loop
- Using Index in While Loop
- Recursion
- Reverse Order
- Quiz That Stores In a List of Dictionaries
name = "Kalani Cabral-Omana"
print("name", name, type(name))
# variable of type integer
age = 16
print("age", age, type(age))
# variable of type float
score = 100.0
print("score", score, type(score))
print()
# variable of type list (many values in one variable)
langs = ["Python", "JavaScript", "Java"]
print("langs", langs, type(langs))
print("- langs[0]", langs[0], type(langs[0]))
print()
# variable of type dictionary (a group of keys and values)
person = {
"name": name,
"age": age,
"score": score,
"langs": langs
}
print("person", person, type(person))
print('- person["name"]', person["name"], type(person["name"]))
Shoes = []
# Append to List a Dictionary of key/values related to a person and cars
Shoes.append({
"Shoe": "Nike",
"Model": "Dunk",
"Year": "2016",
"Color": "Blue",
})
Shoes.append({
"Shoe": "Vans",
"Model": "Classic",
"Year": "2020",
"Color": "Black",
})
Shoes.append({
"Shoe": "Converse",
"Model": "High-Top",
"Year": "2021",
"Color": "Blck",
})
Shoes.append({
"Shoe": "Nike",
"Model": "Blazers",
"Year": "2020",
"Color": "White",
})
Shoes.append({
"Shoe": "Adidas",
"Model": "Yeezy",
"Year": "2023",
"Color": "White",
})
Shoes.append({
"Shoe": "Adidas",
"Model": "Yeezy",
"Year": "2022",
"Color": "Black",
})
Shoes.append({
"Shoe": "Nike",
"Model": "Trainers",
"Year": "2021",
"Color": "Red",
})
# Print the data structure
print(Shoes)
InfoDb = []
# Append to List a Dictionary of key/values related to a person and cars
InfoDb.append({
"FirstName": "Kalani",
"LastName": "Cabral-Omana",
"DOB": "April 18",
"Residence": "San Diego",
"Email": "kcabralomana@gmail.com.com",
"Owns_Shoes": ["Nike Dunks, Black Vans, Black Converse, Nike Blazers"]
})
# Partner
# Append to List a 2nd Dictionary of key/values
InfoDb.append({
"FirstName": "Navan",
"LastName": "Yatavelli",
"DOB": "October 31",
"Residence": "San Diego",
"Email": "navany@gmail.com",
"Owns_Shoes": ["White Yeezy, Yeezy Slides"]
})
# Print the data structure
print(InfoDb)
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", "Shoes: ", end="") # end="" make sure no return occurs
print(", ".join(d_rec["Owns_Shoes"])) # join allows printing a string list with separator
print()
# for loop iterates on length of InfoDb
def for_loop():
print("For loop output\n")
for i in range(len(InfoDb)):
print_data(InfoDb[i])
for_loop()
def while_loop():
print("While loop output\n")
i = 0
while i < len(InfoDb):
record = InfoDb[i]
print_data(record)
i += 1
return
while_loop()
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)
list = ["Vans","Converse","Yeezy","Blazers","Slides","Trainers"]
for i in list:
print(i[::-1])
print(list[::-1])
def question_with_response(prompt):
print("Question: " + prompt)
word = " "
questions = 4 # number of questions
correct = 0
questions_answers = [{"What command is used to include other functions that were previously developed?" : "import",
"What command is used to evaluate correct or incorrect response in this example?" : "if",
"Each 'if' command contains an '_________' to determine a true or false condition?" : "expression",
"Variables for the values the function needs. Is passed as an argument when the function is called" : "parameters"}] # dictionary
# questions_answers.append = ({"What command is used to include other functions that were previously developed?" : "import",
# "What command is used to evaluate correct or incorrect response in this example?" : "if",
# "Each 'if' command contains an '_________' to determine a true or false condition?" : "expression",
# "Variables for the values the function needs. Is passed as an argument when the function is called" : "parameters"}) # dictionary
for i in questions_answers:
for question, answer in i.items():
question_with_response(question) # printing the questions
word = input("Answer: ") # variable that connects to the user's input
if word == answer: # if the answer provided is correct
print("You got it right!!")
correct += 1
else: # if the answer provided is wrong
print("Your answer was wrong")
print(str(correct) + "/" + str(questions)) # correct/len(questions_answers)