0

所以我最近在看一个关于如何创建刽子手的python教程。我试过了,但它的源代码有一些问题,所以我一直在尝试编写运行良好的类似代码。我最近才遇到一个问题。基本上,每当您猜测“秘密”字符串中的最后一个字母时,它都不会用该字母替换空格。我想知道为什么会发生这种情况以及如何解决它。

这是代码

import random
words = '''aardvark baboon calf camel deer dingo alligator ant jackal iguana 
falcon flamingo macaw manatee marmoset flee emu narwhal leopord ocelot 
lemming opossum dog dolphin dove beaver bison wallaby rabbit salamander 
seagull sheep skunk tiget tortoise unicorn dragon zombie'''.split()

def randomWord(wordList):
    wIndex = random.randint(0, len(wordList) - 1)
    return wordList[wIndex]

def displayGame(wrong, right, secret):
    print('Wrong Guesses:', end=' ')
    for letter in wrong:
        print(letter, end=' ')
    print()

    blanks = '_' * len(secret)

    for i in range(0, len(secret) - 1):
        if secret[i] in right:
            blanks = blanks[:i] + secret[i] + blanks[i+1:]

    for letter in blanks:
        print(letter, end=' ')
    print()

def getGuess(guessed):
    while True:
        guess = input()
        guess = guess.lower()
        if len(guess) != 1:
            print('Please enter a single letter.')
        elif guess in guessed:
            print('You have already guessed that letter.')
        elif guess not in 'abcdefghijklmnopqrstuvwxyz':
            print('Please print a letter.')
        else:
            return guess

def playAgain():
    print('Would you like to play again? (yes or no)', end=' ')
    while True:
        keepGoing = input()
        if keepGoing == 'yes':
            return True
        elif keepGoing == 'no':
            return False
        else:
            print('Yes or no please.')



print('Guessing Game!')
print()
wrong = ''
right = ''
secret = randomWord(words)
gameOver = False
while True:
    while gameOver == False:
        displayGame(wrong, right, secret)

        guess = getGuess(wrong + right)

        if guess in secret:
            right += guess

            foundAll = True
            for i in range(0, len(secret) - 1):
                if secret[i] not in right:
                    foundAll = False
                    break

            if foundAll:
                print('Congratulations! You won! The word was ' + secret)
                gameOver = True
                break

        else:
            wrong += guess

            if len(wrong) > 7:
                print('You ran out of guesses! Game over! The word was ' + secret)
                gameOver = True
                break

    keepGoing = playAgain()
    if keepGoing:
        wrong = ''
        right = ''
        secret = randomWord(words)
        gameOver = False
    else:
        print('Thanks for playing!')
        break

这是损坏的输出

Would you like to play again? (yes or no) yes
Wrong Guesses: 
_ _ _ _ _ _ 
a
Wrong Guesses: 
_ _ a _ _ _ 
m
Wrong Guesses: m 
_ _ a _ _ _ 
l
Wrong Guesses: m l 
_ _ a _ _ _ 
c
Wrong Guesses: m l c 
_ _ a _ _ _ 
e
Wrong Guesses: m l c 
_ e a _ e _ 
r
Wrong Guesses: m l c 
_ e a _ e _ 
d
Wrong Guesses: m l c d 
_ e a _ e _ 
b
Wrong Guesses: m l c d 
b e a _ e _ 
v
Congratulations! You won! The word was beaver
4

1 回答 1

1

问题出在

for i in range(0, len(secret) - 1)

默认情况下,范围不包括最后一个值,并且您1要从中减去

于 2013-04-05T05:10:00.960 回答