5 total points
Directions¶
Due: 10/12/2025 @ 11:59 p.m.
Change the name of your notebook to EX6_FirstLast.ipynb, where First is your first name and Last is your last name.
For each of the following prompts, write or modify Python code that fulfills the requirements.
Loop troubleshooting¶
If you start running a while loop and it does not immediately print the output and stop, you likely have an “infinite loop”.
The best way to stop this is to press the “stop” button repeatedly, or to click on the top right dropdown menu by “RAM and Disk” and click Disconnect and Delete Runtime. You can then restart the notebook by pressing the “Go” arrow again after you make changes to your code.
for loops will not have this problem (unless you make them extremely large).
while loops¶
Problem 1¶
Modify the initialization variable i so that only 2, 3, 4, 5 is printed (0.5 pts).
i = 1
while i < 6:
print(i)
i = i + 11
2
3
4
5
Problem 2¶
Move the increment code i = i + 1 so that 2, 3, 4, 5, 6 is printed. Only move the line, do not change the code (0.5 pts).
i = 1
while i < 6:
print(i)
i = i + 11
2
3
4
5
Problem 3¶
Write a while loop that prints out the numbers 1, 3, 5, 7 by modifying the following example (0.5 pts).
i = 0
while i < 6:
print(i)
i = i + 10
1
2
3
4
5
for loops¶
Problem 4¶
Modify the list (and only the list) so that only the numbers 1, 2, 3, 4 are printed (0.5 pts).
a = [1, 2, 3, 4, 5]
for i in a:
print(i)1
2
3
4
5
Problem 5¶
Use range to print all int numbers 1 through 10 (0.5 pts).
Problem 6¶
Add enumerate to the following loop example to print out the index if the iteration value is equal to 5 using an if / else statement (0.5 pts).
a = [1, 2, 3, 4, 5]
for i in a:
print(i)1
2
3
4
5
dict iterators¶
In the following problems, you must use one of these dictionary iterators: keys, values, or items.
You must include the message building code within the loop, and it should work for any sized dict.
Problem 7¶
Use one iterator loop using the defined dict named obs to print out the following messages (1 pt):
The temperature on Jan 1 was 15 F
The temperature on Jan 2 was 16 F
The temperature on Jan 3 was 17 Fobs = {'Jan 1': 15, 'Jan 2': 16, 'Jan 3': 17}
Problem 8¶
Use one iterator loop using the defined dict named obs to print out the following conditional messages using an if / else statement (1 pt):
The temperature on Jan 1 was 45 F
The temperature on Jan 2 was 32 F. It was freezing!
The temperature on Jan 3 was 15 F. It was freezing!obs = {'Jan 1': 45, 'Jan 2': 32, 'Jan 3': 15}