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 ?

Akaaya
  • 3
  • 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. – DSLima90 May 03 '17 at 10:35

1 Answers1

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)
DSLima90
  • 2,680
  • 1
  • 15
  • 23
  • 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 ? – Akaaya May 03 '17 at 11:46
  • 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.... – DSLima90 May 03 '17 at 11:52
  • Also, if this answer is right and helped you, may you accept it as the right answer? – DSLima90 May 03 '17 at 11:53
  • Take a look at: http://stackoverflow.com/questions/16073396/breaking-while-loop-with-function . THere is an example there with the exception method i mentioned. – DSLima90 May 03 '17 at 11:59
  • Sorry, I thought that upvoting your answer was setting it as the right one... Thanks for your help, have a nice day ! – Akaaya May 03 '17 at 11:59
  • Thank you very much for this link, it is exactly what I was looking for ! But you are right, a simple break will be enough for my code. – Akaaya May 03 '17 at 12:03