如何编写代码让程序查找多个实例 - Python

0 投票
5 回答
1082 浏览
提问于 2025-04-17 00:14

我有这段代码:

Words = ['python','candy', 'banana', 'chicken', 'pizza', 'calculus',
     'cheeseburger', 'binder', 'computer', 'pencil', 'school'
     'artist', 'soccer', 'tennis', 'basketball', 'panda',
     'zebra', 'horse', 'cereal', 'alphabet', 'understand']

number = raw_input('Enter a 1 through 20: ')
x = list()
find = list(Words[int(number)-1])
notherword = list(Words[int(number)-1])
l = list(len(find)*'_')
print 'Your word is', len(find)*'_ '
playing = True
while playing:
    letter = raw_input('Please pick a letter ')
    if letter in find:
        a = find.index(str(letter))
        l[int(a)] = letter
        q = (' ')
        j = q.join(l)
        print j
        find[a] = ('_')
        if l == notherword:
            print 'You win!!!'
            playing = False
    else:
        print 'Strike ' +str(len(x)+1) +str('.') +str(' Not a letter in the word')
        x.append(letter)
        if len(x) > 4:
            print 'Game Over x('
            playing = False

这是一个猜单词的游戏,叫做“吊死鬼”。你先选择一个数字,然后这个数字对应一个单词,游戏就开始了。但是当我输入“banana”这个单词时,它只找到第一个“a”,而没有找到其他的“a”。我该怎么写代码才能同时找到多个相同的字母,这样游戏才能顺利进行呢?

我更新了代码

5 个回答

1

你需要用一个循环来逐个匹配每个a:

while playing:
    letter = raw_input('Please pick a letter ')
    while letter in find:
        a = find.index(letter)

        # Clear the letter so we can match the next instance.
        find[a] = None 

        l[a] = letter
        q = (' ')
        j = q.join(l)
        print j
        if l == find:
            print 'You win!!!'
            playing = False
    else:
        ....

[ 这并不是很常见,但在Python中,你可以在while循环中使用else。 ]

另外,你应该在游戏循环之前设置x = list(),因为现在这样的话,你永远不会输。

1

一个简单的解决办法是把找到的字母换成其他的,这样就不会被找到两次了。你可以用列表推导式来获取某个字母的所有位置:

if letter in find:
    # use a list comprehension to get the index of all occurrences of a given letter
    indexes = [i for i, char in enumerate(find) if char == letter]
    # update found and l accordingly
    for i in indexes:
        find[i] = None
        l[i] = letter

然后要检查他们是否获胜,你可以这样做:

if '_' not in l:
    print 'You win!!!'

你还需要在 while 循环外面创建 x,而不是每次玩家猜错时都重新创建,这样玩家才能真正输掉(你也可以用 while True 然后用 break 来结束,而不是使用 playing 变量):

x = list()
while True:
    ...
    else:
        print 'Not a letter in the word'
        x.append(letter)
        if len(x) > 4:
            print 'Game Over'
            break

顺便说一下,你在循环中不需要使用 strint。另外,''.join() 是一个常见的 Python 写法,你应该使用这个。下面是一个修订版,考虑了以上内容:

Words = ['python','candy', 'banana', 'chicken', 'pizza', 'calculus',
     'cheeseburger', 'binder', 'computer', 'pencil', 'school'
     'artist', 'soccer', 'tennis', 'basketball', 'panda',
     'zebra', 'horse', 'cereal', 'alphabet', 'understand']

number = raw_input('Enter a 1 through 20: ')

find = list(Words[int(number)-1])
l = list(len(find)*'_')
x = list()

print 'Your word is', len(find)*'_ '

while True:
    letter = raw_input('Please pick a letter ')
    if letter in find:
        indexes = [i for i, char in enumerate(find) if char == letter]
        for i in indexes:
            find[i] = None
            l[i] = letter
        print ' '.join(l)
        if '_' not in l:
            print 'You win!!!'
            break
    else:
        print 'Not a letter in the word'
        x.append(letter)
        if len(x) > 4:
            print 'Game Over'
            break
1

你应该把这个

if letter in find:
    a = find.index(str(letter))
    l[int(a)] = letter

换成这个

letter_in_word = False
for a,c in enumerate(find):
    if c == letter:
        l[a] = letter
        letter_in_word = True
if letter_in_word:
    ...
else:
    ...

撰写回答