1

I am trying to make a Python script which counts the amount of letters in a randomly chosen word for my Hangman game.

I already looked around on the web, but most thing I could find was count specific letters in a word. After more looking around I ended up with this, which does not work for some reason. If someone could point out the errors, that'd be greatly appreciated.

wordList = ["Tree", "Fish", "Monkey"]
wordChosen = random.choice(wordList)
wordCounter = wordChosen.lower().count['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
print(wordCounter)
Bakuriu
  • 98,325
  • 22
  • 197
  • 231
SikuliXUser
  • 53
  • 1
  • 2
  • 9
  • Please, if the code you are running raises an error you should post the error in the question too. Especially if the code you post isn't complete. – Bakuriu Aug 24 '15 at 18:14
  • The code is complete... – SikuliXUser Aug 24 '15 at 22:18
  • No it isn't. It raises a `NameError`. In any case even if the code is complete it's just simpler to also post the error message. For example if I'm using my mobile to look at the question I could provide an answer, but if I need to run the code things get a lot harder on a mobile... just saying: if you want to get the answer as fast as possible you want to allow as much people as possible to answer. Not providing the error message text reduces the amount of people that can answer easily (and note that mobile is getting more and more used even on SO...) – Bakuriu Aug 25 '15 at 06:40

3 Answers3

6

Are you looking for collections.Counter?

>>> import collections
>>> print(collections.Counter("Monkey"))
Counter({'M': 1, 'y': 1, 'k': 1, 'o': 1, 'n': 1, 'e': 1})
>>> print(collections.Counter("Tree"))
Counter({'e': 2, 'T': 1, 'r': 1})

>>> c = collections.Counter("Tree")
>>> print("The word 'Tree' has {} distinct letters".format(len(c)))
The word 'Tree' has 3 distinct letters
>>> print("The word 'Tree' has {} instances of the letter 'e'".format(c['e']))
The word 'Tree' has 2 instances of the letter 'e'
Kevin
  • 74,910
  • 12
  • 133
  • 166
1

First off, your code contains an error that is rather important to understand:

wordChosen.lower().count['a', 'b'] #...

count is a function and so it requires you to surround its parameters with parentheses and not square brackets!

Next you should try to refer to Python Documentation when using a function for the first time. That should help you understand why your approach will not work.

Now to address your problem. If you want to count the number of letters in your string use len(wordChosen) which counts the total number of characters in the string.

If you want to count the frequencies of each letter a few methods have already been suggested. Here is one more using a dictionary:

import string
LetterFreq={}
for letter in string.ascii_lowercase:
    LetterFreq[letter] = 0
for letter in wordChosen.lower():
    LetterFreq[letter] += 1

This has the nice perk of defaulting all letters not present in the word to a frequency of 0 :)

Hope this helps!

Michael S Priz
  • 1,116
  • 7
  • 17
0

Problem:
The count method only takes in one argument and you are trying to pass a whole list.

Solution:
Simply iterate over all the letters, then test if they are in the string before you print them and their amount.

import random
wordList = ["Tree", "Fish", "Monkey"]
wordChosen = random.choice(wordList)

letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

for letter in letters:
    if letter in wordChosen.lower():
        amount = str(wordChosen.lower().count(letter))
        print(letter + " : " + amount)

Result:
If the random word chosen is "Tree":

e : 2
r : 1
t : 1

Conclusion:
Using collections is definitely a more effective method, but I believe the way I have shown above creates more of the output you were looking for.

ThatGuyRussell
  • 1,361
  • 9
  • 18