简体   繁体   English

即使设置为False,布尔值也不会停止条件

[英]Boolean does not stop a condition even if set as False

For this code : 对于此代码:

for crop in database:
    print("The current crop is :", crop)
    x.all_crop_parameters_match_the_PRA_ones = True     

    while x.all_crop_parameters_match_the_PRA_ones :

        ASSESS_Tmin( crop, x, PRA)

        print("x.all_crop_parameters_match_the_PRA_ones = ",  x.all_crop_parameters_match_the_PRA_ones)

        ASSESS_Water( crop, PRA, x)

        print("x.all_crop_parameters_match_the_PRA_ones = ",  x.all_crop_parameters_match_the_PRA_ones)

        ASSESS_pH(crop, PRA, x)

I get the following results: 我得到以下结果:

The current crop is : FBRflx
Checking minimum Temperatures...
x.all_crop_parameters_match_the_PRA_ones =  False

Checking the Water Resources...
Verifying if the Water Resources match with the Tmin supported by the crop...
x.all_crop_parameters_match_the_PRA_ones =  False

The soil pH of this PRA matches to the crop requirements.
This crop is edible for the current PRA !

I don't understand why the programm see that x.all_crop_parameters_match_the_PRA_ones is False and still runs the next functions instead of breaking the loop and switching to the next crop. 我不明白为什么程序会看到x.all_crop_parameters_match_the_PRA_ones为False,并且仍然运行下一个函数,而不是中断循环并切换到下一个作物。

x is a class that contains all the variables I use and modify is several functions of my code. x是包含我使用和修改的所有变量的类,这是我代码的几个函数。 Could it be an error because the boolean comes from a class ? 因为布尔值来自一个类,这可能是一个错误吗?

What you post look's like the expected behavior to me. 您发布的内容看起来就像我的预期行为。 The while loop is going reevaluate the condition only when it executes all code inside it... while循环仅在执行其中的所有代码时才重新评估条件...

If you want the while code to break in the middle, try putting breaks: 如果希望while代码在中间中断,请尝试放置中断:

for crop in database:
    print("The current crop is :", crop)
    x.all_crop_parameters_match_the_PRA_ones = True     

    while x.all_crop_parameters_match_the_PRA_ones :

        ASSESS_Tmin( crop, x, PRA)

        print("x.all_crop_parameters_match_the_PRA_ones = ",  x.all_crop_parameters_match_the_PRA_ones)

        if not x.all_crop_parameters_match_the_PRA_ones:
            break

        ASSESS_Water( crop, PRA, x)

        print("x.all_crop_parameters_match_the_PRA_ones = ",  x.all_crop_parameters_match_the_PRA_ones)

        if not x.all_crop_parameters_match_the_PRA_ones:
            break

        ASSESS_pH(crop, PRA, x)

Note that it is going to be in a loop the time x.all_crop_parameters_match_the_PRA_ones == True . 请注意,时间x.all_crop_parameters_match_the_PRA_ones == True将会成环。 If you want to execute the code inside while just once, instead of in a loop, you can try: 如果你想里面执行的代码while只有一次,而不是在一个循环中,你可以试试:

for crop in database:
    print("The current crop is :", crop)
    x.all_crop_parameters_match_the_PRA_ones = True     

    ASSESS_Tmin( crop, x, PRA)

    print("x.all_crop_parameters_match_the_PRA_ones = ",  x.all_crop_parameters_match_the_PRA_ones)

    if not x.all_crop_parameters_match_the_PRA_ones:
        continue

    ASSESS_Water( crop, PRA, x)

    print("x.all_crop_parameters_match_the_PRA_ones = ",  x.all_crop_parameters_match_the_PRA_ones)

    if not x.all_crop_parameters_match_the_PRA_ones:
        continue 

    ASSESS_pH(crop, PRA, x)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM