Letter guessing game in python

时光怂恿深爱的人放手 提交于 2019-12-11 17:57:21

问题


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!


回答1:


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:

  1. str.join() method.
  2. range() built-in function.
  3. generator expressions (used to generate N underscores).

All in all, what it does is:

  1. Count the characters in the word 'jabberwocky'
  2. Generate n underscores
  3. Join them by a space.



回答2:


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



来源:https://stackoverflow.com/questions/15775920/letter-guessing-game-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!