Skip to main content

Section 2.13 Unit 2b Loops Coding Practice Exercises

90 minutes
This section contains coding practice exercises using loops. There are 3 subsections to practice with Turtle graphics, picture manipulation, and number calculations.
These are exercises mostly written by Dr. Barb Ericson in the AP CSP and Python for Everybody ebooks.

Subsection 2.13.1 Turtle Loop Practice

Activity 2.13.1.

The code currently draws a square. Change it so that it draws a triangle. Do so by modifying the loop condition and the turn that the turtle makes without modifying other code.
Remember that to make a complete shape, the turns must sum to 360 degrees.

Activity 2.13.2.

Fix the errors in the code so that it draws an octagon (8-sided polygon). There are 4 bugs to fix.
Remember that to make a complete shape, the turns must sum to 360 degrees.

Activity 2.13.3.

Fix the indention in the code below to correctly draw 20 pentagons (5-sided polygons) in a snowflake design. The second loop should be inside the first loop.
Make sure to use four spaces for each indentation level (the tab key will automatically use 4 spaces).

Subsection 2.13.2 Picture Loop Practice

Data: puppies.jpg

Activity 2.13.4.

The code below sets all the red pixels to 0. Change it so that it sets all the green pixels to 0 instead.

Activity 2.13.5.

Write code to set the red value of each pixel to 255. This should give the entire image a rosy tint.

Activity 2.13.6.

This program should make a black-and-white image by averaging the red, green, and blue values of each pixel and then setting the red, green, and blue values to that average.

Subsection 2.13.3 Number Loop Practice

Activity 2.13.7.

Complete the code to create a program that adds up all the numbers from 10 to 20. Make sure to include both 10 and 20 in the sum.

Activity 2.13.8.

Complete the code to calculate the sum of the squares of all numbers from 1-10 (inclusive). The sum of squares is the result of squaring each value before adding it to the sum. The sum of squares for the numbers 1-4 would be \(1 * 1 + 2 * 2 + 3 * 3 + 4 * 4 = 30\text{.}\)

Activity 2.13.9.

Below is the start of a program to calculate how long it will take to have at least $50,000 if you invest $300 per month at 0.5% interest per month (approximately 6% per year).
You should add code to continue doing the following until money reaches 50000:
  • Increase the existing money by 0.5% (multiply it by 1.005). (This is interest earned on all money already invested.)
  • Add the monthly investment amount to money. (This is another month of investment.)
  • Increase the months count by 1
Remember the debugging tricks you used - use codelens or print out values to make sure your work is correct.
You have attempted of activities on this page.