猜字游戏在python [英] Letter guessing game in python

查看:412
本文介绍了猜字游戏在python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在python中编写一个基本的字母游戏。在游戏中,计算机主持人从可能的单词列表中挑选一个单词。每个玩家(计算机AI和人)显示一系列空白,每个字母一个字。每个玩家然后猜测一个字母和一个位置,并被告知以下之一:

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)该信属于该位置(最好的结果)

1) That letter belongs in that position (the best outcome)

2)那封信在字里,但不在那个位置

2) That letter is in the word, but not in that position

3)那封信不在剩余的空格

3) That letter is not in any of the remaining blank spaces

当单词完全显示时,玩家猜出最多的字母正确地赢了一个点。计算机主持人选择另一个字并重新开始。第一个玩家获得5分的胜利。
在基本游戏中,两个玩家共享相同的一组空白,他们填充,所以玩家受益于对方的工作。

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.

-

我有一个随机词来得很好。现在,我想计算字中的字符数,所以我知道有多少空格要指示。我该怎么做呢?

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?

一旦正常工作,我认为将玩家/ AI猜测与该单词进行比较并不会太难,并相应地继续。

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.

谢谢!

推荐答案

字词中的字元数: len() 功能

Number of characters in the word: len() function

In [15]: len('jabberwocky')
Out[15]: 11

面具的示例:

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


  1. str.join() 方法。

  2. range() -in函数。

  3. 生成器表达式(用于生成N个下划线。)

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

总之,它所做的是:


  1. 计算'jabberwocky'字符中的字符

  2. 生成下划线


这篇关于猜字游戏在python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆