Section3.6Lists 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.
The following code should take a list list1 and returns a new list with the first and last items from the original list swapped. For example, if list1 = [3, 2, 1, 4], create newList = [4, 2, 1, 3].
The following code should take a list of words, word_list and create and print a password which is a string with the first and last character of each word in a list. For example, ["show", "me", "the", "money"] would print swmetemy. Put the code blocks into the correct order.
The following code should take a list with the amount of rain for each day and calculate the average rainfall. However, there are some problems with the rain measurement equipment, so we need to ignore days that have a negative number for the amount of rain. Only add the rain values that are not negative to the sum. For example, rainfall = [30,-2, 0] should calculate Average: 15. Put the code blocks into the correct order.
The following code should take a list of prices and create a new list with the original prices reduced by half. Limit each price to two digits after the decimal by rounding to the closest value. For example, price_list = [10.5, 20.75, 30.10] should print [5.25, 10.38, 15.05].
The following code should take a list of words, word_list and create a new list with only the words in word_list that have the letter a in them. For example, word_list = ["a", "pretty", "cat"] should print ["a", "cat"].
word_list = ["a", "pretty", "cat"]
result = []
---
for word in word_list:
---
if "a" in word:
---
if word == "a": #paired
---
result.append(word)
---
word_list.append(result) #paired
---
print(result)