16.7. Chapter Exercises

Write code for the function printFirst. It should print the first numberItems from list with one item appearing on each line.

The tests for you code will check the output - make sure not to print anything extra or remove anything from the main program.

Improve your code for the printFirst you wrote in the previous exercise.

Before printing, make sure that numberItems is not greater than the length of the list. If numberItems is OK, do the printing. If not, just print "Invalid numItems".

The tests for you code will check the output - make sure not to print anything extra or remove anything from the main program.

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 just worry about the either fixing just the “too high” or just the “too low” values first, then worry about the other category once that is working.

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