看板 Python 關於我們 聯絡資訊
大家好 之前在上c的時候助教每次都只有說 要結束的話 return 0就好 但現在在寫python的基準題就遇到一些困難 例如 =============== def f(n): if xxxx: else: return aa ================== 常常會在if 後面少寫return造成bug 又或者 =============== def f(a, b): if xxxx: return a * b else: return return a * b ================== 這樣兩個return result是可以的嗎 還是只要下面這樣就好呢? 最近在寫基礎題的時候 真的是被難倒了 ============ def f(a, b): if xxxx: result = a*b else: return return result ================== -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 59.120.118.229 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1449812232.A.F2A.html
Thisisnotptt: 碰到第一個return就會跳出了,多放幾個不會有問題的 12/11 14:38
Thisisnotptt: python中不特別放也不會怎樣,只是通則是建議要有 12/11 14:39
Thisisnotptt: 假設你覺得function裡面很多return不好看 12/11 14:40
Thisisnotptt: 可於if成立時讓result=a*b, else的時候result=None 12/11 14:42
Thisisnotptt: 最後再return result 12/11 14:42