7.14 Exercises
From Foundations of Python ProgrammingGNU FDL 1.3Original pagePython1 class period6 exercises3 graded tests
7.14 Exercises
Read first in the book: 7.7 Traversal and the for Loop: By Index, 7.8 Nested Iteration: Image Processing, 7.9 Printing Intermediate Results, 7.10 Naming Variables in For Loops, 7.11 The Gory Details: Iterables, 7.12 Keeping Track of Your Iterator Variable and Your Iterable, 7.13 Glossary.
Exercise 1
In Robert McCloskey's book Make Way for Ducklings, the names of the ducklings are Jack, Kack, Lack, Mack, Nack, Ouack, Pack, and Quack. This loop tries to output these names in order.
Of course, that's not quite right because Ouack and Quack are misspelled. Can you fix it?
Open the
01folder and editmain.py. Run Tests checks it.
Exercise 2
Get the user to enter some text and store it in a variable called user_input then print it out in reverse order.
Open the
02folder and editmain.py. Run Tests checks it.
Exercise 3
Write a program that uses a for loop to print
| One of the months of the year is January
| One of the months of the year is February
| One of the months of the year is March
| etc ...
Open the
03folder and editmain.py. This one is not graded, so check it yourself.
Exercise 4
Assume you have a list of numbers 12, 10, 32, 3, 66, 17, 42, 99, 20
a. Write a loop that prints each of the numbers on a new line. b. Write a loop that prints each number and its square on a new line.
Open the
04folder and editmain.py. Run Tests checks it.
Exercise 5
Write a program that asks the user for the number of sides, the length of the side, the color, and the fill color of a regular polygon. The program should draw the polygon and then fill it in.
Open the
05folder and editmain.py. This one is not graded, so check it yourself.
Exercise 6
A drunk pirate makes a random turn and then takes 100 steps forward, makes another random turn, takes another 100 steps, turns another random amount, etc. A social science student records the angle of each turn before the next 100 steps are taken. Her experimental data is 160, -43, 270, -97, -43, 200, -940, 17, -86. (Positive angles are counter-clockwise.) Use a turtle to draw the path taken by our drunk friend. After the pirate is done walking, print the current heading. Assume that the turtle originally has a heading of 0 and accumulate the changes in heading to print out the final. Your solution should work for any sequence of experimental data.
Open the
06folder and editmain.py. This one is not graded, so check it yourself.
Check your understanding: a mixed-up code puzzle in the book.
Check your understanding: a mixed-up code puzzle in the book.
For teachers
7.14 Exercises
Students read Foundations of Python Programming's “Exercises” in CS Room and work through its 6 Python coding exercises (3 graded).
- Time: 1 class period
- Tests: 3 in 3 graded exercises, set up for you when you import the lesson
Reading first
- 7.7 Traversal and the for Loop: By Index
- 7.8 Nested Iteration: Image Processing
- 7.9 Printing Intermediate Results
- 7.10 Naming Variables in For Loops
- 7.11 The Gory Details: Iterables
- 7.12 Keeping Track of Your Iterator Variable and Your Iterable
- 7.13 Glossary
Exercises
| Folder | Grading | Tests |
|---|---|---|
01 | Autograded | 1 |
02 | Autograded | 1 |
03 | Not graded; students check it themselves | 0 |
04 | Autograded | 1 |
05 | Not graded; students check it themselves | 0 |
06 | Not graded; students check it themselves | 0 |