Python 函数中的 return 失效了是什么鬼

2018-10-26 20:12:46 +08:00
 evanshh
# coding:utf-8
import numpy as np


def drop_water(y, x, array, path):
    s1 = array[y + 1, x - 1] * 5  # 左下
    s2 = array[y + 1, x] * 4  # 下
    s3 = array[y + 1, x + 1] * 3  # 右下
    s4 = array[y, x + 1] * 2  # 右
    s5 = array[y, x - 1] * 1  # 左
    l = [s1, s2, s3, s4, s5]
    w = 4 if sum(l) in [0, 15] else max(l)
    if w == 1:
        next_s = [y, x - 1]
    elif w == 2:
        next_s = [y, x + 1]
    elif w == 3:
        next_s = [y + 1, x + 1]
    elif w == 4:
        next_s = [y + 1, x]
    elif w == 5:
        next_s = [y + 1, x - 1]
    path.append(next_s)
    if next_s[0] == array.shape[0] - 1:
        print(path)
        return path
    else:
        print('a')
        drop_water(next_s[0], next_s[1], array, path)
        print('w')


array = np.array([[1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
                   1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1,
                   1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1],
                  [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
                   1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1,
                   1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1],
                  [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0,
                   0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
                   1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1],
                  [1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0,
                   0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
                   1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1]])

s = drop_water(0, 30, array, [])
print(s)

输出如下:

a
a
[[1, 30], [2, 30], [3, 29]]
w
w
None
3040 次点击
所在节点    Python
21 条回复
ericls
2018-10-26 20:14:56 +08:00
你没有 return 呀
evanshh
2018-10-26 20:16:06 +08:00
@ericls 在 if 里
ericls
2018-10-26 20:17:30 +08:00
@evanshh 只要进入了 else 就 return 不了了 只剩副作用了
evanshh
2018-10-26 20:18:41 +08:00
@ericls 问题是执行 if 语句后 return 会退出函数,不会执行后续的 else 了呀
ericls
2018-10-26 20:20:26 +08:00
@evanshh 第一次 call 如果进入了 else 就只剩副作用了
awanabe
2018-10-26 20:21:32 +08:00
path 都没打印出来..说明没有走到 if 里面...
判断逻辑先看看吧...
evanshh
2018-10-26 20:22:10 +08:00
@awanabe [[1, 30], [2, 30], [3, 29]]这显然就是 path..
bucky
2018-10-26 20:23:11 +08:00
这个问题,王垠都说过了,if else 要成对出现,保证你的逻辑完整,要不然你都发现不了你的 bug 在哪里
ericls
2018-10-26 20:23:52 +08:00
@evanshh 那是副作用里面的 if
evanshh
2018-10-26 20:24:29 +08:00
@ericls 副作用是啥?
omph
2018-10-26 20:24:53 +08:00
用调试走一遍都清楚了
evanshh
2018-10-26 20:25:08 +08:00
@bucky 我的 if else 成对出现了呀,能否详细解释一下?
evanshh
2018-10-26 20:25:52 +08:00
@omph 走了好几遍仍然懵逼才来问的
ericls
2018-10-26 20:27:42 +08:00
@evanshh 简单说 就是你的 else 里面 没有 return
Zzdex
2018-10-26 20:30:07 +08:00
递归不是你这么玩的
zsdroid
2018-10-26 20:32:24 +08:00
建议百度“递归”先
ysc3839
2018-10-26 20:34:52 +08:00
从输出能明显看出代码执行到了 else 里面,而你 else 里面没有 return。
zsdroid
2018-10-26 20:35:16 +08:00
第 1 次调用 drop_water,进入 else 打印 a
再次调用 drop_water (第 2 次),进入 else 打印再次 a
再次调用 drop_water (第 3 次),进入 if 打印 path
打印第 2 次调用的 w
打印第 1 次调用的 w
然后没有 return 所以最后输出 none
evanshh
2018-10-26 20:38:40 +08:00
@ericls 好了想通了,进了逻辑死胡同了
awanabe
2018-10-26 20:43:19 +08:00
递归最后一次走到 else 里面...

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/501535

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX