19.4 Exercises
From Foundations of Python ProgrammingGNU FDL 1.3Original pagePython1 class period2 exercises3 graded tests
19.4 Exercises
Read first in the book: 19.2 When to use try/except, 19.3 Standard Exceptions.
Exercise 1
Below, we have provided buggy code. Add a try/except clause so the code runs without errors. If a blog post didn't get any likes, a 'Likes' key should be added to that dictionary with a value of 0.
Open the
01folder and editmain.py. Run Tests checks it.
Exercise 2
The code below assigns the 5th letter of each word in food to the new list fifth. However, the code currently produces errors. Insert a try/except clause that will allow the code to run and produce of list of the 5th letter in each word. If the word is not long enough, it should not print anything out. Note: The pass statement is a null operation; nothing will happen when it executes.
Open the
02folder and editmain.py. Run Tests checks it.
For teachers
19.4 Exercises
Students read Foundations of Python Programming's “Exercises” in CS Room and work through its 2 Python coding exercises (2 graded).
- Time: 1 class period
- Tests: 3 in 2 graded exercises, set up for you when you import the lesson
Reading first
Exercises
| Folder | Grading | Tests |
|---|---|---|
01 | Autograded | 2 |
02 | Autograded | 1 |