Skip to content

Instantly share code, notes, and snippets.

@mayankdawar
Created February 19, 2020 18:41
Show Gist options
  • Save mayankdawar/3d5be107c2bfa3e75ebfa8b8e1ef74af to your computer and use it in GitHub Desktop.
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.
items = ["whirring", "wow!", "calendar", "wry", "glass", "", "llama","tumultuous","owing"]
count = 0
for i in items:
if 'w' in i:
count += 1
acc_num = count
@Ravi007-asd
Copy link

wow

@Maksimovprojects
Copy link

it doesn't work

@Maksimovprojects
Copy link

use if word.count("w") > 0:

@arielfcotrim
Copy link

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)

@Redwoodce
Copy link

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)

@TL2662
Copy link

TL2662 commented Apr 5, 2024

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