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

2024-09-29 00:16:33 发布

您现在位置:Python中文网/ 问答频道 /正文

对于此代码:

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)

我得到以下结果:

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 !

我不明白为什么程序看到x.all\u crop\u parameters\u match\u the \u PRA\u one是假的,并且仍然运行下一个函数,而不是中断循环并切换到下一个裁剪。你知道吗

x是一个包含我使用的所有变量的类,修改的是代码中的几个函数。这可能是一个错误,因为布尔值来自一个类吗?你知道吗


Tags: the代码cropforismatchonescurrent
1条回答
网友
1楼 · 发布于 2024-09-29 00:16:33

你贴出来的样子和我期望的一样。while循环只有在执行其中的所有代码时才会重新评估条件。。。你知道吗

如果希望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)

请注意,它将在时间x.all_crop_parameters_match_the_PRA_ones == True的循环中。如果只想在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)

相关问题 更多 >