作者checkIO (chec)
看板Python
标题[问题] return的疑惑
时间Fri Dec 11 13:37:10 2015
大家好 之前在上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://webptt.com/cn.aspx?n=bbs/Python/M.1449812232.A.F2A.html
1F:推 Thisisnotptt: 碰到第一个return就会跳出了,多放几个不会有问题的 12/11 14:38
2F:→ Thisisnotptt: python中不特别放也不会怎样,只是通则是建议要有 12/11 14:39
3F:→ Thisisnotptt: 假设你觉得function里面很多return不好看 12/11 14:40
4F:→ Thisisnotptt: 可於if成立时让result=a*b, else的时候result=None 12/11 14:42
5F:→ Thisisnotptt: 最後再return result 12/11 14:42