Reputation: 1933
I'm attempting to code a basic letter game in python. In the game, the computer moderator picks a word out of a list of possible words. Each player (computer AI and human) is shown a series of blanks, one for each letter of the word. Each player then guesses a letter and a position, and are told one of the following:
1) That letter belongs in that position (the best outcome)
2) That letter is in the word, but not in that position
3) That letter is not in any of the remaining blank spaces
When the word has been fully revealed, the player to guess the most letters correctly wins a point. The computer moderator picks another word and starts again. The first player to five points wins the game. In the basic game, both players share the same set of blanks they're filling in, so the players benefit from each other's work.
My Question---
I've got the random word coming in just fine. Now, I'd like to count the number of characters in the word so I know how many spaces to indicate. How would I go about this?
Once that's working properly, I assume it won't be too hard to compare the player/AI guesses with the word and proceed accordingly.
Thanks!
Upvotes: 0
Views: 928
Reputation: 1367
Number of characters in the word: len()
function
In [15]: len('jabberwocky')
Out[15]: 11
An example of a mask:
In [16]: mask = ' '.join(('_' for i in range(len('jabberwocky'))))
In [18]: mask
Out[18]: '_ _ _ _ _ _ _ _ _ _ _'
# j a b b e r w o c k y
References on everything involved in the mask example:
str.join()
method.range()
built-in function.All in all, what it does is:
Upvotes: 2
Reputation: 405
On a cursory analysis of your project, I believe you can find all the answers you need in the Python documentation. Addressing your first question, see here:
http://docs.python.org/3.2/library/functions.html#len
Addressing future questions that you may have as you need to deal with strings, see here: http://docs.python.org/3.2/tutorial/introduction.html#strings
Upvotes: 1