Skip to main content

Exercises 27.10 Chapter Exercises

1.

The following program segment should iterate through the list terms and then add each item to the list vocab if it is not already in vocab. If the word is already in vocab, then the program should add 1 to the variable duplicates.
Arrange and indent the blocks correctly. You will not use them all.

2.

Write code to change the first item to “February”, to add “April” between “March” and “May”, and to remove “August”.

3.

Write code for the function getRange. It should take a list of numbers and return the range (the maximum value minus the minimum value). If we have the list [10, 8, 13, 20, 15], the range would be calculated as 20 - 8 and would be 12.
Hint: You have seen recipes for “find highest” and “find lowest”. You can either use both or combine them into one loop.
Feel free to comment out some of the tests or add your own while you work on your code.
Use codelens and print statements for debugging!

4.

Write code for the function freezeDays. It should take a list of temperatures temps (in degrees F) and return the number of days that were below 32 degrees.
Hint: You may want to start by printing out each temperature and a “freeze” or “no” message to make sure you can identify the right temperatures. Then worry about counting how many there were.
Feel free to comment out some of the tests or add your own while you work on your code.

5.

Write code for the function getGPA. It should figure out the GPA for a list of grades that looks like [“A”, “A”, “C”].
To calculate the GPA, you need to add up the point value of all the grades. An A is 4, a B is 3, a C is 2, a D is 1, and an F is 0. Then divide by the number of grades in the list.
Hint: First write some if logic that just prints the correct value for each grade (4, 4, 2 for [“A”, “A”, “C”]). Then worry about getting the total of them.
Feel free to comment out some of the tests or add your own while you work on your code.
You have attempted of activities on this page.