0

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 is a class that contains all the variables I use and modify is several functions of my code. Could it be an error because the boolean comes from a class ?

1
  • It only stop's in the next loop... Do you want it to stop execution in the middle of the code inside the while? You have to put a explicit break to break it... What you post looks like the expected behavior. Commented May 3, 2017 at 10:35

1 Answer 1

0

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...

If you want the while code to break in the middle, try putting breaks:

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. If you want to execute the code inside while just once, instead of in a loop, you can try:

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)
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks a lot, it works.I actually thought that setting my condition to False in a function inside the loop (for example ASSESS_Tmin) could break the while loop earlier : if I understand well, it is impossible ?
I think the best way is to verify the condition and use the break instruction inside the loop, another way would be to raise an exception and catch it in the upper context... but it is not something I recommend doing in this case. If you are just learning python, maybe you should stick with the breaks....
Also, if this answer is right and helped you, may you accept it as the right answer?
Take a look at: stackoverflow.com/questions/16073396/… . THere is an example there with the exception method i mentioned.
Sorry, I thought that upvoting your answer was setting it as the right one... Thanks for your help, have a nice day !
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.