以"if"表示奇怪的返回语句Python中的语句 [英] Return statements acting weird in "if" statements in Python

查看:137
本文介绍了以"if"表示奇怪的返回语句Python中的语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是关于此声明,我在网上阅读的内容类似"if语句之后的行,并且与if语句将始终运行的缩进相同" ,无论"if语句"是对还是错."我将在下面的示例中对此进行说明.

My question is regarding this statement I read online that went something along the lines of "lines that follow the 'if statement' and are at the same indentation as the "if statement" will always run, regardless whether the 'if statement' is true or false." I'll show that using my examples below.

因此,我有一个将两个数字作为输入并返回两个输入中较大的一个的过程.

So I have this procedure that takes in two numbers as inputs and returns the greater of the two inputs.

因此,在第一个示例中,第4行和第5行代码的缩进级别与"if a> b:"相同,应始终根据上述说明运行.

Therefore, the 4th and 5th lines of code in this first example, which are at the same level of indentation as "if a>b:" should always run according to the statement above.

示例1

def bigger(a,b):
    if a>b:
        x = "the first number is bigger"
    x = "the second number is bigger"
    return x
print bigger(9,7)

Python打印第二个数字更大",因此代码按照我在开始时写的原始语句工作.即使那是错误的,因为9> 7.

Python prints "the second number is bigger," so the code is working according to that original statement I wrote in the beginning. Even though that is false because 9>7.

但是,在以下示例中使用return语句时,我会感到困惑:

But where I run into confusion is when return statements are used in the following example:

示例2

def bigger(a,b):
    if a>b:
        return "the first number is bigger"
    return "the second number is bigger"
print bigger(9,7)

这一次,Python打印第一个数字更大".这是因为9> 7

This time, Python prints "the first number is bigger." This is true because 9>7

我的困惑:第二个示例中的第四行代码返回第二个数字更大""不应该一直运行,因为它与"if语句"处于相同的缩进级别,就像我们看到的那样在示例#1中?

My confusion: Shouldn't the 4th line of code in this second example, "return "the second number is bigger"", always run because it's at the same indentation level of the "if statement," just like we saw in example #1?

这两个代码示例似乎相互矛盾,因为在示例#1中,Python识别出第二个数字更大"行并打印该行,但是在示例#2中,Python忽略了第二个数字更大"行并打印另一条第一个数字较大"行.

It seems like the two examples of code contradict each other because in the example #1, Python recognizes the "second number is bigger" line and prints that line but in example #2, Python ignores the "second number is bigger" line and prints the other "first number is bigger" line.

我试图使这一点尽可能清楚.谢谢

I tried to make this as clear as possible. Thanks

推荐答案

我的困惑:在第二个示例中,不应将代码的第四行, 返回第二个数字更大"",始终运行,因为它位于 与"if语句"的缩进级别相同,就像我们在 示例#1?

My confusion: Shouldn't the 4th line of code in this second example, "return "the second number is bigger"", always run because it's at the same indentation level of the "if statement," just like we saw in example #1?

这是因为当您有一个return语句时,函数执行会中断:

This is because the function execution breaks when you have a return statement:

def bigger(a,b):
    if a>b:
        return "the first number is bigger" # function execution breaks here!!!
    return "the second number is bigger"
print bigger(9,7)

这篇关于以"if"表示奇怪的返回语句Python中的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆