函数调用不正确的值 [英] Function calling incorrect values

查看:74
本文介绍了函数调用不正确的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的函数,重要的一行是我再次调用 f() 的地方.

Here's my function, the important line is where i call f() again.

def f(num,xyz,possible,temp):
    y = num%9
    x = num-y
    print num
    for xyz[num] in possible[num]:
        if ispossibility(temp,xyz[num],x,y) == True:
            temp[num] = xyz[num]
            if num != 80:
                f(num+1,xyz,possible,temp) # i call f() with num incremented
            else:
                print 'yes'
                exit()

程序的输出是

0
1
2
3
4
5
6
7
8
3
2
3
3

问题在于,无论何时调用函数 f(),变量 num 都应始终加 1.但从输出中,调用 f() 时,num 为 3,而之前为 8.所以我想知道这怎么可能(输出应该是 9 btw,而不是 3),当 f() 总是被调用 w/num 增加 1 时.我已经研究这个函数一段时间了,我真的不明白发生了什么.

The problem is that whenever the function f() is called, the variable num should always be incremented by 1. But from the output, f() is called with num as 3, when it was previously eight. And so i am wondering how that is possible (the output should be 9 btw, and not 3) when f() is always called w/ num incremented by 1. I've been looking at this function for some time now and i really don't understand what is happening.

推荐答案

你在循环内部调用 f(num+1, ...) ,所以可以调用 f() 多次使​​用相同的 num 值.

You call f(num+1, ...) inside of a loop, so it is possible to call f() with the same value for num multiple times.

这篇关于函数调用不正确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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