我如何确定A内部的凹痕错误中的问题,由于代码内部的标签或空间引起的ELIF循环。是否有识别它的编译器?

发布于 2025-01-17 12:20:06 字数 704 浏览 1 评论 0原文

我是Python的新手,因此我很难理解循环中的凹痕,因为我学到的其他程序都没有使用过它。如果我们使用括号来定义IF内部的某些块,这不是简单吗?无论如何,以下是代码的屏幕截图,以及以红色标记的错误。

代码的实际行:

if Turb_Wake_State_Model == "GLAUERT":
        if a_ax < 0.333:
            a_ax = C_ax/(4*(1-a_ax))
        else:
            a_ax = C_ax/((4*(1-0.25*a_ax*(5-3*a_ax))
            
    elif Turb_Wake_State_Model == "SPERA":
        a_c = 0.2
        if a_ax < a_c:
            a_ax = C_ax/(4*(1-a_ax))
        else:
            a_ax = (0.25*(C_ax - a_c^2)/(1-2*a_c))
    
    else:
         print ("WRONG TURBULENT WAKE STATE MODEL IS SPECIFIED")

错误发生在ELIF循环。 我尝试了其他所有组合和IFS的组合,但无济于事。还尝试阅读“ IF”陈述的帮助,但实际上并未描述缩进。这个问题对某些人来说似乎很容易,但是我在这里最简单的事情遇到了麻烦。

I am very new to python, so I am having trouble understanding the indentation in loops as no other programs I learnt used it. Isn't it much simpler if we use brackets to define certain blocks inside if and else statements? Anyways below is the screenshot of the code and the error marked in red.

Actual line of CODE:

if Turb_Wake_State_Model == "GLAUERT":
        if a_ax < 0.333:
            a_ax = C_ax/(4*(1-a_ax))
        else:
            a_ax = C_ax/((4*(1-0.25*a_ax*(5-3*a_ax))
            
    elif Turb_Wake_State_Model == "SPERA":
        a_c = 0.2
        if a_ax < a_c:
            a_ax = C_ax/(4*(1-a_ax))
        else:
            a_ax = (0.25*(C_ax - a_c^2)/(1-2*a_c))
    
    else:
         print ("WRONG TURBULENT WAKE STATE MODEL IS SPECIFIED")

The error occurs at the elif loop.
I tried all combinations of else and ifs but to no avail. Also tried reading the help "if" statement but it didn't actually describe the indentations. The problem might seem very trivial to some but i am having trouble with the simplest of things here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ζ澈沫 2025-01-24 12:20:06

我认为您正在寻找这样的东西:

if Turb_Wake_State_Model == "GLAUERT":
    if a_ax < 0.333:
        a_ax = C_ax/(4*(1-a_ax))
    else:
        a_ax = C_ax/((4*(1-0.25*a_ax*(5-3*a_ax))
            
elif Turb_Wake_State_Model == "SPERA":
    a_c = 0.2
    if a_ax < a_c:
        a_ax = C_ax/(4*(1-a_ax))
    else:
        a_ax = (0.25*(C_ax - a_c^2)/(1-2*a_c))
    
else:
    print ("WRONG TURBULENT WAKE STATE MODEL IS SPECIFIED")

在 python 中,您的 elif 和 else 语句必须与 if 处于相同的缩进级别> 您希望它们对应的语句。在最后一个 print() 行之前还有一个额外的空格,Python 对空格非常挑剔。我编写了一个与您的流程结构相同的小程序来演示:

# test.py
def test(teststr1, teststr2):
    if teststr1 == "test1":
        print("test1")
        if teststr2 == "test1":
            print("test1 test1")
        else:
            print("test1 else")

    elif teststr1 == "test2":
        print("test2")
        if teststr2 == "test1":
            print("test2 test1")
        else:
            print("test2 else")

    else:
        print("else")


test("test1", "test1")
test("test1", "foobar")
test("test2", "test1")
test("test2", "foobar")
test("foobar", "foobar")

随着输出:

$ python3 test.py
test1
test1 test1
test1
test1 else
test2
test2 test1
test2
test2 else
else

您可能想看看 此处获取示例和参考。

我也同意括号更有意义,但 python 会 python :P

I think something like this is what you are looking for:

if Turb_Wake_State_Model == "GLAUERT":
    if a_ax < 0.333:
        a_ax = C_ax/(4*(1-a_ax))
    else:
        a_ax = C_ax/((4*(1-0.25*a_ax*(5-3*a_ax))
            
elif Turb_Wake_State_Model == "SPERA":
    a_c = 0.2
    if a_ax < a_c:
        a_ax = C_ax/(4*(1-a_ax))
    else:
        a_ax = (0.25*(C_ax - a_c^2)/(1-2*a_c))
    
else:
    print ("WRONG TURBULENT WAKE STATE MODEL IS SPECIFIED")

In python your elif and else statements have to be on the same indentation level as the if statement you want them to correspond to. You also had an extra space before the last print() line, python is really picky about spaces. I wrote a small program with the same flow structure as yours to demonstrate:

# test.py
def test(teststr1, teststr2):
    if teststr1 == "test1":
        print("test1")
        if teststr2 == "test1":
            print("test1 test1")
        else:
            print("test1 else")

    elif teststr1 == "test2":
        print("test2")
        if teststr2 == "test1":
            print("test2 test1")
        else:
            print("test2 else")

    else:
        print("else")


test("test1", "test1")
test("test1", "foobar")
test("test2", "test1")
test("test2", "foobar")
test("foobar", "foobar")

With output:

$ python3 test.py
test1
test1 test1
test1
test1 else
test2
test2 test1
test2
test2 else
else

You may want to look here for examples and references.

Also I agree brackets make more sense, but python gonna python :P

熊抱啵儿 2025-01-24 12:20:06

elif 和外部 else 的缩进应与外部 if 的缩进匹配。

The indentation of the elif and the outer else should match the indentation of the outer if.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文