Created
February 19, 2020 18:41
-
-
Save mayankdawar/3d5be107c2bfa3e75ebfa8b8e1ef74af to your computer and use it in GitHub Desktop.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
items = ["whirring", "wow!", "calendar", "wry", "glass", "", "llama","tumultuous","owing"] | |
count = 0 | |
for i in items: | |
if 'w' in i: | |
count += 1 | |
acc_num = count |
it doesn't work
use if word.count("w") > 0:
items = ["whirring", "wow!", "calendar", "wry", "glass", "", "llama", "tumultuous", "owing"]
acc_num = 0
for character in items:
if "w" in character:
acc_num += 1
print(acc_num)
items = ["whirring", "wow!", "calendar", "wry", "glass", "", "llama","tumultuous","owing"]
acc_num=0
for y in items:
if (y.count("w"))>0:
acc_num+=1
print (acc_num)
items = ["whirring", "wow!", "calendar", "wry", "glass", "", "llama","tumultuous","owing"]
acc_num =0
for i in items:
if 'w' in i:
acc_num += 1
print(acc_num)
This works.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
wow