Assessment: Week Three >> Python Basics
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.
rainfall_mi = "1.65, 1.46, 2.05, 3.03, 3.35, 3.46, 2.83, 3.23, 3.5, 2.52, 2.8, 1.85"
months = rainfall_mi.split(",")
count = 0
for i in months:
if float(i) > 3.0:
count += 1
num_rainy_months = count
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | ‘for’ | ‘rainf…count’ | Testing that your code has a for loop (Don’t worry about actual and expected values). |
Pass | 5 | 5 | Testing that num_rainy_months has the right value |
You passed: 100.0% of the tests
Score: 1.0 / 1
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.
sentence = """students flock to the arb for a variety of outdoor
activities such as jogging and picnicking"""
sen = sentence.split()
count = 0
# Write your code here.
for i in sen:
if i[0] == i[len(i)-1]:
count += 1
same_letter_count = count
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | 2 | 2 | Checking that same_letter_count has the correct value |
Pass | ‘for ‘ | ‘sente…count’ | Testing that your code has a for loop |
You passed: 100.0% of the tests
Score: 1.0 / 1
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.
items = ["whirring", "wow!", "calendar", "wry", "glass", "", "llama","tumultuous","owing"]
count = 0
for i in items:
if 'w' in i:
count += 1
acc_num = count
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | ‘ in ‘ | ‘items…count’ | Testing that you are using the in operator. |
Pass | 4 | 4 | Testing that acc_num has been set to the number of strings that have ‘w’ in them. |
You passed: 100.0% of the tests
Score: 1.0 / 1
Comment: autograded
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.
sentence = "python is a high level general purpose programming language that can be applied to many different classes of problems."
sen = sentence.split()
count = 0
for i in sen:
if 'a' in i or 'e' in i:
count += 1
num_a_or_e = count
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | ‘ in ‘ | ‘sente…count’ | Testing that you are using the in operator. |
Pass | 14 | 14 | Testing that num_a_or_e has been set to the correct number. |
You passed: 100.0% of the tests
Score: 1.0 / 1
Comment: autograded
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
.
s = "singing in the rain and playing in the rain are two entirely different situations but both can be fun"
vowels = ['a','e','i','o','u']
count = 0
# Write your code here.
for i in s:
if i in vowels:
count += 1
num_vowels = count
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | 32 | 32 | testing whether num_vowels is set correctly |
Pass | ‘for’ | ‘s = “…count’ | Testing that you are using a for loop. |
You passed: 100.0% of the tests