Python List Dictionaries Iteration
My Python List Dictionaries Iteration
name = "Ben Lee"
print("name", name, type(name))
# variable of type integer
age = 17
print("age", age, type(age))
# variable of type float
score = 94.5
print("score", score, type(score))
print()
# variable of type list (many values in one variable)
langs = ["Python", "JavaScript", "Java", "Bash", "R"]
print("langs", langs, type(langs), "length", len(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"]))
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": "Ben",
"LastName": "Lee",
"DOB": "January 19",
"Sports": "Basketball and golf",
"Email": "bencabrallee@gmail.com",
"Owns_Cars": ["2018-Toyota IM"]
})
# Append to List a 2nd Dictionary of key/values
InfoDb.append({
"FirstName": "Nicolas",
"LastName": "Mosqueda",
"DOB": "July 19",
"Sports": "Got cut from baseball team",
"Email": "nicomosqueda02@gmail.com",
"Owns_Cars": ["Nissan"]
})
# 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", "Sports:", d_rec["Sports"]) # \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 loop algorithm iterates on length of InfoDb
def for_loop():
print("For loop output\n")
for record in InfoDb:
print_data(record)
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)
def new_game():
guesses = []
correct_guesses = 0
question_num = 1
for key in questions:
print("-------------------------")
print(key)
for i in options[question_num-1]:
print(i)
guess = input("Enter (A, B, C, or D): ")
guess = guess.upper()
guesses.append(guess)
correct_guesses += check_answer(questions.get(key),guess)
question_num += 1
display_score(correct_guesses, guesses)
# -------------------------
def check_answer(answer, guess):
if answer == guess:
print("CORRECT!")
return 1
else:
print("WRONG!")
return 0
# -------------------------
def display_score(correct_guesses, guesses):
print("-------------------------")
print("RESULTS")
print("-------------------------")
print("Answers: ", end="")
for i in questions:
print(questions.get(i), end =" ")
print()
print("Guesses: ", end="")
for i in guesses:
print(i, end =" ")
print()
score = int((correct_guesses/len(questions))*100)
print("Your score is: "+str(score)+"%")
# -------------------------
def play_again():
response = input("Do you want to play again?: (yes or no): ")
response = response.upper()
if response == "YES":
return True
else:
return False
# -------------------------
questions = {
"Who won the 2021-2022 NBA Championship?: ": "A",
"How many championships do the Lakers have?: ": "B",
"How many games are in a NBA playoff series ?: ": "C",
"Which player has won 5 championships?: ": "A",
}
options = [["A. Warriors", "B. Celtics", "C. Lakers", "D. Bucks"],
["A. 12", "B. 17", "C. 20", "D. 8"],
["A. 5", "B. 3", "C. 7", "D. 1"],
["A. Kobe Bryant", "B. Michael Jordan", "C. Lebron James", "D. Kevin Durant"]]
new_game()
while play_again():
new_game()
print("Bye!")