0

Okay so here is a list:

sentList = ['I am a dog', 'I am a cat', 'I am a house full of cards']

I want to be able to count the total number of times a user inputted letter appears throughout the entire list.

userLetter = input('Enter a letter: ')

Let's say the letter is 'a'

I want the program to go through and count the number of times 'a' appears in the list. In this case, the total number of 'a' in the list should be 8.

I've tried using the count function through a for loop but I keep getting numbers that I don't know how to explain, and without really knowing how to format the loop, or if I need it at all.

I tried this, but it doesn't work.

count = sentList.count(userLetter)

Any help would be appreciated, I couldn't find any documentation for counting all occurrences of a letter in a list.

Dakota Brown
  • 57
  • 1
  • 2
  • 7
  • Possible duplicate of [Count occurrence of a character in a string](https://stackoverflow.com/questions/1155617/count-occurrence-of-a-character-in-a-string) – Kaushik NP Sep 26 '17 at 05:15

6 Answers6

4

Use the sum() builtin to add up the counts for each string in the list:

total = sum(s.count(userLetter) for s in sentList)
aghast
  • 14,785
  • 3
  • 24
  • 56
2

Merge all the strings into a single string and then use the count function.

count = ''.join(sentList).count(userLetter)
Ankush Bhatia
  • 133
  • 1
  • 1
  • 6
0

Your approach is halfway correct. The problem is that you need to go through the list.

word_count=0
for l in sentList:
     word_count+= l.count(userLetter)
Kaushik NP
  • 6,733
  • 9
  • 31
  • 60
0

Have you tried something like this?

userLetter = input('Enter a letter: ')
sentList = ['I am a dog', 'I am a cat', 'I am a house full of cards']

letterCount = 0
for sentence in sentList:
    letterCount += sentence.count(userLetter)

print("Letter appears {} times".format(letterCount))
Joe Davy
  • 713
  • 7
  • 11
  • The key to understanding this is that "for sentence in sentList" gives you an object called sentence which you can use inside your for loop. That object is equal to the value of each element of the list, which in this case is a string. We can do string.count(letter) to get the number of times that letter appears in that string. By adding that value to letterCount, and repeating for each sentence we've got our answer! – Joe Davy Sep 26 '17 at 05:22
  • Awesome explanation! Thank you! – Dakota Brown Sep 26 '17 at 05:23
0

word_list = ["aardvark", "baboon", "camel"] import random chosen_word = random.choice(word_list) guess = input("Guess a letter: ").lower()

for letter in chosen_word: if letter == guess: print("Right") else: print("Wrong")

  • 2
    Try formatting your answer. Here a small how-to https://stackoverflow.com/help/formatting . – Oskar Mar 17 '22 at 19:59
0

example program to check if entered letter is present in random choice of word

word_list = ["aardvark", "baboon", "camel"]

import random

chosen_word = random.choice(word_list)

guess = input("Guess a letter: ").lower()

for letter in chosen_word:

    if letter == guess:

        print("Right")
    else:

        print("Wrong")
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 17 '22 at 22:10