23.3. Ch 5 Practice: Looping through lists and dictionaries#
Try out these coding problems to practice looping, lists, and dictionaries
Make a loop that displays “Are you awake yet?” 5 times
for i in range(5):
display("Are you awake yet?")
'Are you awake yet?'
'Are you awake yet?'
'Are you awake yet?'
'Are you awake yet?'
'Are you awake yet?'
Make a list of names (at least three), and save it in a variable called names
names = ["Kyle", "Susan", "Another person"]
Now loop over each of those names, and for each name display “[name] is awesome!”
for name in names:
display(name + " is awesome!")
'Kyle is awesome!'
'Susan is awesome!'
'Another person is awesome!'
Now, do the same thing as before, but for each name, first make a string that has “[name] is awesome!” and save it in a variable, then use the .upper()
function on the string to make it all uppercase and save it into a variable, then display the final string.
for text in names:
sentence = text + " is awesome!"
upper_sentence = sentence.upper()
display(upper_sentence)
'KYLE IS AWESOME!'
'SUSAN IS AWESOME!'
'ANOTHER PERSON IS AWESOME!'
Now, we are going to make a dictionary with information on a photo
photo_1_info = {
"width": 800,
"height": 600,
"location": "that one mountain",
"device": "iPhone 6"
}
Select and display the width of the photo
photo_1_info["width"]
800
Select and display the location of the photo
photo_1_info["location"]
'that one mountain'
Now we are going to make a list of photo info for you to go through
photo_info_list = [
{
"width": 800,
"height": 600,
"location": "that one mountain",
"device": "iPhone 6"
},
{
"width": 800,
"height": 600,
"location": "on the lake",
"device": "iPhone 5"
},
{
"width": 1600,
"height": 800,
"location": "The underground mines",
"device": "Nokia 3310"
}
]
Now, make a for loop to go through each set of phone info in photo_info_list
, and for each one, use print
commands to display the width, height, location, and device
for photo_info in photo_info_list:
print(photo_info["width"])
print(photo_info["height"])
print(photo_info["location"])
print(photo_info["device"])
print()
800
600
that one mountain
iPhone 6
800
600
on the lake
iPhone 5
1600
800
The underground mines
Nokia 3310