Skip to main content

Section 6.14 Chapter Assessment

Check your understanding

Checkpoint 6.14.1.

rainfall_mi is a string that contains the average number of inches of rainfall in Michigan for every month (in inches) with every month separated by a comma. Write code to compute the number of months that have more than 3 inches of rainfall. Store the result in the variable num_rainy_months. In other words, count the number of items with values > 3.0.
Hard-coded answers will receive no credit.

Checkpoint 6.14.2.

The variable sentence stores a string. Write code to determine how many words in sentence start and end with the same letter, including one-letter words. Store the result in the variable same_letter_count.
Hard-coded answers will receive no credit.

Checkpoint 6.14.3.

Write code to count the number of strings in list items that have the character w in it. Assign that number to the variable acc_num.
HINT 1: Use the accumulation pattern!
HINT 2: the in operator checks whether a substring is present in a string.
Hard-coded answers will receive no credit.

Checkpoint 6.14.4.

Write code that counts the number of words in sentence that contain either an β€œa” or an β€œe”. Store the result in the variable num_a_or_e.
Note 1: be sure to not double-count words that contain both an a and an e.
HINT 1: Use the in operator.
HINT 2: You can either use or or elif.
Hard-coded answers will receive no credit.

Checkpoint 6.14.5.

Write code that will count the number of vowels in the sentence s and assign the result to the variable num_vowels. For this problem, vowels are only a, e, i, o, and u. Hint: use the in operator with vowels.

Checkpoint 6.14.6.

Create one conditional so that if β€œFriendly” is in w, then β€œFriendly is here!” should be assigned to the variable wrd. If it’s not, check if β€œFriend” is in w. If so, the string β€œFriend is here!” should be assigned to the variable wrd, otherwise β€œNo variation of friend is in here.” should be assigned to the variable wrd. (Also consider: does the order of your conditional statements matter for this problem? Why?)

Checkpoint 6.14.7.

We have written conditionals for you to use. Create the variable x and assign it some integer so that at the end of the code, output will be assigned the string "Consistently working".

Checkpoint 6.14.8.

Write code so that if "STATS 250" is in the list schedule, then the string "You could be in Information Science!" is assigned to the variable resp. Otherwise, the string "That's too bad." should be assigned to the variable resp.

Checkpoint 6.14.9.

Create the variable z whose value is 30. Write code to see if z is greater than y. If so, add 5 to y’s value, otherwise do nothing. Then, multiply z and y, and assign the resulting value to the variable x.

Checkpoint 6.14.10.

For each string in wrd_lst, find the number of characters in the string. If the number of characters is less than 6, add 1 to accum so that in the end, accum will contain an integer representing the total number of words in the list that have fewer than 6 characters.

Checkpoint 6.14.11.

Checkpoint 6.14.12.

Checkpoint 6.14.13.

Given the following code, what operator should be used so the code works as intended? (Currently states the user is not of legal age)
MANITOBA_LEGAL_AGE = 18
my_age = 18
if my_age > MANITOBA_LEGAL_AGE:
print("You are of legal age in Manitoba")
else:
print("You are underage in Manitoba")

Checkpoint 6.14.14.

Given num = 49 on line 3, what value of i will cause line 14 to run?

Note 6.14.15.

Prime numbers are numbers greater than 1 that only have two factors, 1 and the number itself. Notice that in order to reach line 14, line 12 must evaluate to true. Walk through the code to determine what is happening here and what the value will be when line 12 becomes true.
You have attempted of activities on this page.