Skip to main content

Exercises 28.7 Chapter Exercises

1.

Write code for the function printFirst. It should print the first numberItems from list with one item appearing on each line.
Hint: You will want to use an index-based loop (for i in range( ________ )) to do the iteration.
The tests for your code will check the output - make sure not to print anything extra or remove anything from the main program.

2.

Write the code for the makeAcronym function. It should take a list of words, like ["As", "Far", "As", "I", "Know"] and turn it into a list that has just the first letters of each word: ["A", "F", "A", "I", "K"]
You will need to use an index-based loop (for i in range( ________ )). For each index i, set item i from list to be just its first letter. You may want to do this in multiple steps - first get the current word, then get its first letter, then store that letter as list[i]
Automated tests will check the output - make sure not to print anything extra or remove anything from the main program.

3.

We have a list of numbers that are all supposed to be 8-bit values (0-255).
Write code for the fixNumbers function. It should go through the list that is passed as a parameter and change any value above 255 to 255 and any value below 0 to 0.
You may want to first focus on either fixing just the “too high” or just the “too low” values first, and then worry about the other category once that is working.

4.

Write the code for middleThird(). It should accept a list as its parameter and then slice the list into three equal parts (first third, middle third, last third) and return the middle third.
You can assume the list will always be of a size that evenly divides by 3.
Hint: Indexes you use in a list slice must be integers. You can’t ask for a slice [2.0:4.0], just [2:4]. When you do division, use // to get an integer answer instead of / which gives a decimal answer.
If you are having issues, first calculate the indexes you will use to make the slice and print them out.
You have attempted of activities on this page.