试图制作一个刽子手游戏。无法使我的playerLives变量正确减少

时间:2017-05-04 17:06:19

标签: python

我正在尝试制作一个刽子手游戏。到目前为止,当它循环通过秘密字并返回结果时,我已经非常有效地工作了。每次提交错误的答案时,我的playerLives变量都正确递减,但是当我试图整理我的代码时,它已经停止工作,我不能在我的生活中让它降低价值。

def checkSecretWord(secretWord, guess, playerLives):
letterConfirm = False
letterDeny = False
for i in range(0, len(secretWord)):

    if secretWord[i] == guess:
        letterConfirm = True

    if guess not in secretWord:
        letterDeny = True


if letterConfirm:
    print("Correct Guess!")

if letterDeny:
    print("Incorrect Guess! Try again!")
    playerLives -= 1
    return playerLives




def gameRunning():
    gameActive = True
    secretWord = getSecretWord()
    guessed_letters = []
    playerLives = 6
    while gameActive:
        makeBoard(playerLives, secretWord, guessed_letters)
        guess = checkLetter(guessed_letters)
        checkSecretWord(secretWord, guess, playerLives)
        print("You have {} lives reminaing".format(playerLives))

checkSecretWord()函数在秘密字中搜索用户输入的字母('guess')。它正确返回“不正确的猜测!再试一次!”当letterConfirm为真时(也就是说,字母与秘密字中的字母不匹配),但它似乎没有像我想要的那样将变量'playerLives'的值减少1。

编辑:只是为了掩盖自己的背影 - 我知道“返回玩家生活= =玩家生活 - 1”没有任何意义,只是写得很糟糕,但这是我最后一次绝望的尝试,然后我求助于他。

编辑2:感谢大家的帮助。问题分类和经验教训。欢呼声。

1 个答案:

答案 0 :(得分:0)

你在这里遇到了一些问题

你的第一个功能是空的。让我们修复

上的标签
def checkSecretWord(secretWord, guess, playerLives):
    letterConfirm = False
    letterDeny = False
    for i in range(0, len(secretWord)):

        if secretWord[i] == guess:
            letterConfirm = True

        if guess not in secretWord:
            letterDeny = True


    if letterConfirm:
        print("Correct Guess!")
        return playerLives  #THIS IS THE BIG CHANGE

    if letterDeny:
        print("Incorrect Guess! Try again!")
        return playerLives - 1 #THIS IS THE BIG CHANGE

你像玩全局变量一样对待玩家,你需要重写这个以便我们可以正确增加玩家数量

def gameRunning():
    gameActive = True
    secretWord = getSecretWord()
    guessed_letters = []
    playerLives = 6
    while gameActive:
        makeBoard(playerLives, secretWord, guessed_letters)
        guess = checkLetter(guessed_letters)
        playerLives = checkSecretWord(secretWord, guess, playerLives) # see "the big changes" I made earlier
        print("You have {} lives reminaing".format(playerLives))