为什么While循环返回一个变量而不是另一个?

时间:2016-05-10 23:05:27

标签: python

我正在尝试自学Python并正在建立一个基本的刽子手游戏。我有一点路,但现在我被卡住了。

我试图把大部分的计数器放在'猜测一个函数,然后引用另一个函数来实际判断猜测是否正确。

所以这是我的反函数:

def game(generated_word):
    hangman_lst = []
    guess_num = 0
    for i in range(0, len(generated_word)):
        hangman_lst.append("__")
    print(" ".join(hangman_lst))

    while "__" in hangman_lst:
        if guess_num == 0:
            l = input("This is your first guess. Guess a letter!")
            guess(l, random_word_stripped, hangman_lst, guess_num)
        elif guess_num == 9:
            print("Sorry - you lose!")
            print("The word was " + str(random_word_stripped))
            break
        else:
            l = input("This is guess #" + str(guess_num + 1) + ":")
            guess(l, random_word_stripped, hangman_lst, guess_num)

    if "__" not in hangman_lst:
        print("**Ta-da!** You're a winner!")

这是我的功能,以确定猜测是否正确:

def guess(ltr, word, lst, try_num):
    upper_ltr = ltr.upper()
    if len(upper_ltr) > 1:
        print("That's more than 1 letter, try again:")
        print(" ".join(lst))
    elif len(upper_ltr) < 1:
        print("You didn't enter a letter, try again:")
        print(" ".join(lst))
    elif upper_ltr in lst:
        print("You already guessed that letter, try again:")
        print(" ".join(lst))
    elif upper_ltr not in word:
        print("Sorry, that's incorrect. Try again.")
        print(" ".join(lst))
        try_num += 1
        return try_num
    else:
        for n in range(0, len(word)):
            if word[n] == upper_ltr:
                lst[n] = upper_ltr
        print("Nice job. That's one. You get a bonus guess.")
        print(" ".join(lst))
        return lst

所以它有点工作,因为刽子手的单词会被猜到的字母更新。但我无法让计数器工作 - guess_num似乎总是保持在0,所以它总是循环通过这部分:

if guess_num == 0:
    l = input("This is your first guess. Guess a letter!")
    guess(l, random_word_stripped, hangman_lst, guess_num)

但是,lst函数好像返回guess,所以为什么不是try_num

干杯!

2 个答案:

答案 0 :(得分:1)

您需要增加guess_num。在while循环结束时添加guess_num = guess_num + 1会解决这个问题,但是你只希望guess_num在猜测错误时增加,所以你会做这样的事情。 (我还没有测试过这个)

while "__" in hangman_lst:
    if guess_num == 0:
        l = input("This is your first guess. Guess a letter!")
        if guess(l, random_word_stripped, hangman_lst, guess_num):
            print("good guess")
        else:
            guess_num += 1
    elif guess_num == 9:
        print("Sorry - you lose!")
        print("The word was " + str(random_word_stripped))
        break
    else:
        l = input("This is guess #" + str(guess_num + 1) + ":")
        if guess(l, random_word_stripped, hangman_lst, guess_num):
            print("good guess")
        else:
            guess_num += 1

然后在您的猜测函数中,您需要添加行return Truereturn False以返回相应的True / False值,具体取决于猜测是否有效。

所以猜测函数的部分就是

if len(upper_ltr) > 1:
    print("That's more than 1 letter, try again:")
    print(" ".join(lst))

需要更改为

if len(upper_ltr) > 1:
    print("That's more than 1 letter, try again:")
    print(" ".join(lst))
    return False

然后你将为每个if条件执行此操作。一旦python看到一个return语句,它将退出该函数并返回你告诉它返回的任何内容,在我们的例子中将是True或False。

答案 1 :(得分:1)

当您使用return关键字时,表示您正在尝试将计算值返回给函数的调用者。因为return你给调用者一个值,所以你希望在函数完成后将该值赋给某些东西......你没有做的事情。

如果你不明白,这是一个例子......

假设我的main函数调用了一个名为add()的函数,并说我希望这个函数计算传入的两个整数的值。然后,我必须return计算出的值回到呼叫者处继续使用。

def main():
    a = 5
    b = 20
    sum = add(a, b)
    print(sum)

def add(num1, num2):
    return num1 + num2

然后,此代码会打印a + b的值,或者在这种情况下,25