推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
evanshh
V2EX  ›  Python

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

  •  1
     
  •   evanshh · Oct 26, 2018 · 4360 views
    This topic created in 2929 days ago, the information mentioned may be changed or developed.
    # 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
    
    ericls
    ericls
    Oct 26, 2018 via iPhone
    1
    你没有 return 呀
    evanshh
    evanshh
    OP
    Oct 26, 2018
    2
    @ericls 在 if 里
    ericls
    ericls
    Oct 26, 2018 via iPhone
    3
    @evanshh 只要进入了 else 就 return 不了了 只剩副作用了
    evanshh
    evanshh
    OP
    Oct 26, 2018
    4
    @ericls 问题是执行 if 语句后 return 会退出函数,不会执行后续的 else 了呀
    ericls
    ericls
    Oct 26, 2018 via iPhone
    5
    @evanshh 第一次 call 如果进入了 else 就只剩副作用了
    awanabe
    awanabe
    Oct 26, 2018
    6
    path 都没打印出来..说明没有走到 if 里面...
    判断逻辑先看看吧...
    evanshh
    evanshh
    OP
    Oct 26, 2018
    7
    @awanabe [[1, 30], [2, 30], [3, 29]]这显然就是 path..
    bucky
    bucky
    Oct 26, 2018
    8
    这个问题,王垠都说过了,if else 要成对出现,保证你的逻辑完整,要不然你都发现不了你的 bug 在哪里
    ericls
    ericls
    Oct 26, 2018 via iPhone
    9
    @evanshh 那是副作用里面的 if
    evanshh
    evanshh
    OP
    Oct 26, 2018
    10
    @ericls 副作用是啥?
    omph
    omph
    Oct 26, 2018
    11
    用调试走一遍都清楚了
    evanshh
    evanshh
    OP
    Oct 26, 2018
    12
    @bucky 我的 if else 成对出现了呀,能否详细解释一下?
    evanshh
    evanshh
    OP
    Oct 26, 2018
    13
    @omph 走了好几遍仍然懵逼才来问的
    ericls
    ericls
    Oct 26, 2018 via iPhone❤️2
    14
    @evanshh 简单说 就是你的 else 里面 没有 return
    Zzdex
    Zzdex
    Oct 26, 2018
    15
    递归不是你这么玩的
    zsdroid
    zsdroid
    Oct 26, 2018
    16
    建议百度“递归”先
    ysc3839
    ysc3839
    Oct 26, 2018 via Android
    17
    从输出能明显看出代码执行到了 else 里面,而你 else 里面没有 return。
    zsdroid
    zsdroid
    Oct 26, 2018❤️1
    18
    第 1 次调用 drop_water,进入 else 打印 a
    再次调用 drop_water (第 2 次),进入 else 打印再次 a
    再次调用 drop_water (第 3 次),进入 if 打印 path
    打印第 2 次调用的 w
    打印第 1 次调用的 w
    然后没有 return 所以最后输出 none
    evanshh
    evanshh
    OP
    Oct 26, 2018 via Android
    19
    @ericls 好了想通了,进了逻辑死胡同了
    awanabe
    awanabe
    Oct 26, 2018
    20
    递归最后一次走到 else 里面...
    Hombin
    Hombin
    Oct 27, 2018
    21
    else:
    print('a')
    p = drop_water(next_s[0], next_s[1], array, path)
    print('w')
    return p

    增加一个返回应该就可以了~
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5242 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 03:19 · PVG 11:19 · LAX 19:19 · JFK 22:19
    ♥ Do have faith in what you're doing.