def foo():
if 1 > 0:
print('Works first condition!')
return print('Works second condition!') # This is a 'else' block which supposed not to be
# executed cause of the above 'if' condition is True,but
# despite the code executes both of them and return None!`
print(foo())
输出:在第一个条件下工作! 工作第二个条件! 无
如果问题看起来很愚蠢,请注意我是 python 新手
您实际上没有 else 语句,因此 return(与 else 不同)在 if 完成时运行。试试这个:
if 1 > 0:
print('Works first condition!')
else:
print('Works second condition!')