为什么函数中的“else”块有效,而“if”块却为True?

问题描述 投票:0回答:1
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 新手

python-3.x function if-statement return return-value
1个回答
0
投票

您实际上没有 else 语句,因此 return(与 else 不同)在 if 完成时运行。试试这个:

    if 1 > 0:
        print('Works first condition!')
    else:    
        print('Works second condition!') 
© www.soinside.com 2019 - 2024. All rights reserved.