Skip to main content

Section 2.12 Unit 2 Loops Mixed-Up Coding Practice Exercises

Try to solve each of the following. Click the Check button to check each solution. You will be told if your solution is too short, has a block in the wrong order, or you are using the wrong block.
These are exercises mostly written by Dr. Barb Ericson from the AP CSP and Python for Everybody ebooks on Runestone.

Activity 2.12.1.

Create a loop to calculate the sum of all of the odd numbers between 10 to 20 (including 20).

Activity 2.12.2.

The following program segment should print the value of n as long as n is less than 5. But the blocks have been mixed up and include an extra block that isnโ€™t needed in the solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the โ€œCheck Meโ€ button to check your solution.

Activity 2.12.3.

The following program segment should print 0, 2, 4, 6 on separate lines by using a while loop. Arrange the code so that accum prints before increasing its value. The blocks have been mixed up and include an extra block that isnโ€™t needed in the solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the โ€œCheck Meโ€ button to check your solution.

Activity 2.12.4.

The following program segment should print all even numbers from 0 to 8 (this includes 0 and 8). But the blocks have been mixed up and include an extra block that isnโ€™t needed in the solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the โ€œCheck Meโ€ button to check your solution.

Activity 2.12.5.

The following program segment should calculate and print the sum of all numbers between 0 and 30 (including 30). Start by initializing the variable sum. The blocks have been mixed up and include an extra block that isnโ€™t needed in the solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the โ€œCheck Meโ€ button to check your solution.

Activity 2.12.6.

The following program segment should calculate the sum of all odd numbers between 0 and 30. Start by initializing the variable sum and loop through the odd numbers. The blocks have been mixed up and include an extra block that isnโ€™t needed in the solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the โ€œCheck Meโ€ button to check your solution.

Activity 2.12.7.

The following program should find the sum of every multiple of 3 between 3 and 36 and print the sum after each addition. But the blocks have been mixed up and include an extra block that isnโ€™t needed in the solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the โ€œCheck Meโ€ button to check your solution.

Activity 2.12.8.

The following program should print the numbers 5 to 1 starting with 5. But the blocks have been mixed up and include an extra block that isnโ€™t needed in the solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the โ€œCheck Meโ€ button to check your solution.

Activity 2.12.9.

The following program calculates the height of an object as it falls from 1000 feet. The program should use a loop to print out the current height, then update the time by one second and calculate a new height. It should stop looping once the object reaches the ground (height 0) and then print out the final number of seconds.
Put the blocks in the right order and indentation. As usual, you will not use all the blocks.
You have attempted of activities on this page.