-2

Basically i wanted to know how to count the number of letters in a list, for example lets say i add the word 'EXAMPLE' to a list and then lets say i wanna find out how many times the letter 'e' is used given e as the user input how would i write it so it says there are 2 e's in the word 'EXAMPLE'.

So far i got

WordList = []
print("1. Enter A Word")
print("2. Check Letter Or Vowel Times")


userInput = input("Please Choose An Option: ")
if userInput == "1":
    wordInput = input("Please Enter A Word: ")
    WordList.append(wordInput.lower())
TheProKi
  • 13
  • 1
  • 3
  • If you want to count the letters in a single word, you need some way of selecting the right word from `WordList`. – John Gordon Nov 15 '17 at 16:12

2 Answers2

1

I isolated your problem. Use collections' Counter for this:

from collections import Counter

wordInput = input("Please Enter A Word: ").lower()
wordDict = Counter(wordInput) # converts to dictionary with counts

letterInput = input("Please Enter A Letter: ").lower() 

print(wordDict.get(letterInput,0)) # return counts of letter (0 if not found)
Anton vBR
  • 18,287
  • 5
  • 40
  • 46
0
l = ['example']

def count(y):
    for x in l:
        return x.count(y)

count('e')

Without using built-in function count()

l=list('example')

def count(y):
    cnt=0
    for x in l:
        if x == y:
            cnt+=1
    return cnt

print(count('e'))
Van Peer
  • 2,127
  • 2
  • 25
  • 35