Skip to main content

Section 1.11 Unit 1 Coding Practice Exercises

90 minutes
These are exercises written by Dr. Barb Ericson in the AP CSP ebook and grading added by the Welcome to CS ebook on Runestone.

Activity 1.11.1.

There are 2 syntax errors in the following code. Fix it to work correctly without errors.

Activity 1.11.2.

The code below is supposed to make alex move North 150 pixels and then move East 75, but the lines are in the wrong order. Fix it so that it runs properly. Remember that the turtle is facing east (right) at the start.

Activity 1.11.3.

There are 2 syntax errors (errors that prevent the code from running) in the following code. There is also a missing space in one of the strings that will be printed. Fix the code so that it prints exactly this: β€œYour name is Carly and your favorite color is red.”.

Activity 1.11.4.

Run the code below to see the errors.Fix the code to print correctly without errors. It should print, β€œYour name is Carly and your age is 16.”
Note: Don’t forget that to turn an int into a string you do something like str(x) where x is the int you want to turn into a string.

Activity 1.11.5.

Use string slicing to get β€œgiant alligator” from sentence and store it in s1 .
You should not be typing the string β€œgiant alligator” yourself, you should be getting the right part of what is stored in sentence.
The print will put |’s around your output to make it clear if you have a space at the start or end of s1.

Activity 1.11.6.

== is an operator that checks for equality. If the value on the left side is equal to the value on the right side, it gives True. Otherwise it gives False.
Insert the correct operators in place of the #’s so each line prints True. Don’t change any of the numbers.

Activity 1.11.7.

Add a set of parentheses to the expression x = 6 * 1 - 2 so that the code below prints -6 instead of 4.

Activity 1.11.8.

Add parentheses to x = 12 * 2 - 3 + 4 * 2 so that it prints -4 instead of 29.

Activity 1.11.9.

Complete the code on lines 3 and 5 below to print the cost of a car trip of 500 miles when the car gets 26 miles per gallon and gas costs 3.45 a gallon. In line 3, you need to determine the number of gallons the care used if they went 500 miles and the car gets 26 miles per gallon, but make sure you use the variables in your formula. In line 5, compute the cost of the trip using numGallons and pricePerGallon.

Activity 1.11.10.

Assume it is currently 10:00 AM. Complete the code to tell what time it is going to be in 18 hours (using 12-hour time, not 24-hour time). Use the % (modulo or remainder operator) to calculate the correct value for clockTime based on the newTime value that is already calculated.
The answer should be 4 because 28 hours divided into 12-hour chunks leaves a remainder of 4. But make sure not to hardcode the answer - you need to calculate the value, not just type it in.

Activity 1.11.11.

Finish the code on lines 2 and 3 in the code below to print how many hours and minutes you have been waiting when you have been waiting a total of 270 minutes.
Use the % (modulo or remainder operator) and the integer division operator // to calculate the correct values for numHours and numMinutes based on the totalMinutes value.
The correct answers should be 4 for numHours and 30 for numMinutes. But make sure to calculate those values - don’t hardcode them!
You have attempted of activities on this page.