Python递归函数错误地执行return语句? [英] Python recursive function executing return statement incorrectly?

查看:77
本文介绍了Python递归函数错误地执行return语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个涉及 2 个玩家的简单游戏.每个玩家都有两个计数器,他们可以通过选择它们来单独移动.玩家 1 有计数器 1 和 1,而玩家 2 有计数器 3 和 4.为了防止玩家移动对手的计数器之一,我编写了以下递归函数.

I am working on a simple game which involves 2 players. Each player has two counters which they can move individually by selecting them. Player 1 has counters 1 and 1, whilst player 2 has counters 3 and 4. In order to prevent a player from moving one of their opponents counters I have written the following recursive function.

如果没有人作弊",那就没问题了.

It works fine if no one 'cheats'.

如果玩家作弊,该功能会让他们重新输入正确的计数器号码.这将按预期进行到最终的 return 语句.

If a player cheats, the function gets them to re-enter a correct counter number. This is carried through to the final return statement as expected.

然而,此时它似乎没有返回正确的计数器编号,而是采取了另一步骤并将其更改为初始错误值.就好像代码已经记住了在递归过程中尝试过的变量 counter 的所有值,并在最后循环遍历它们,然后返回第一个值.

However instead of returning a correct counter number at this point it appears to take another step and change it to the initial wrong value. It is as if the code has remembered all the values of the variable counter that have been tried during the recursion and cycles through them at the end before returning whatever was the first one.

我错过了什么?

def get_counter(current_player):
    counter = int(input("Select which counter you want to move."))
    if counter != 1 and counter != 2 and current_player == 1:
        print("This is not your counter")
        print("Your counters are 1 or 2")
        get_counter(current_player)
    elif counter != 3 and counter != 4 and current_player == 2:
        print("This is not your counter")
        print("Your counters are 3 or 4")
        get_counter(current_player)
    return counter

推荐答案

您正在递归调用函数,但不返回递归结果.添加 return 语句:

You are calling the function recursively but don't return the recursive result. Add return statements:

def get_counter(current_player):
    counter = int(input("Select which counter you want to move."))
    if counter != 1 and counter != 2 and current_player == 1:
        print("This is not your counter")
        print("Your counters are 1 or 2")
        return get_counter(current_player)
    elif counter != 3 and counter != 4 and current_player == 2:
        print("This is not your counter")
        print("Your counters are 3 or 4")
        return get_counter(current_player)
    return counter

没有显式返回递归get_counter()调用的结果,调用函数从调用前停止的地方继续,并执行return counter语句.本地 counter 变量在递归函数调用之间不共享,因此它是玩家做出的第一选择,最终从最外层调用返回.

Without explicitly returning the result of the recursive get_counter() call, the calling function continues where it had left off before the call, and executes the return counter statement instead. The local counter variable is not shared between recursive function calls, so it's the first choice the player made that is eventually returned from the outermost call.

但是,不要低估您的用户继续尝试作弊的能力;你最终会遇到最大递归深度.您不应该真正使用递归来处理用户输入;改用循环:

However, don't underestimate the ability of your users to continue to try and cheat; you'll eventually run into the maximum recursion depth. You shouldn't really use recursion to handle user input; use a loop instead:

def get_counter(current_player):
    while True:
        counter = int(input("Select which counter you want to move."))
        if current_player == 1 and counter not in (1, 2):
            print("This is not your counter")
            print("Your counters are 1 or 2")
            continue
        if current_player == 2 and counter not in (3, 4):
            print("This is not your counter")
            print("Your counters are 3 or 4")
            continue
        return counter

仅当输入了正确的计数时才返回;如果输入不正确,它会继续循环(重新提出问题).

This returns only if a correct count has been entered; it continues with the loop (reasking the question) if incorrect entries have been made instead.

这篇关于Python递归函数错误地执行return语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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