mercredi 18 novembre 2020

Python: Program still passes through ELSE statement even if the IF condition has been met

I encountered a weird problem that has been hindering me from proceeding with my program. Below, I wrote a WHILE statement that loops through a block of codes until the variable scount is no longer greater than 0.

scount = 42 # this is a sample data only
half = scount / 2     
while scount > 0:
    if half > 0:
        scount -= half
        half -= half
    else:
        scount = 0

On the first loop, the program will see that the variable half is greater than 0. Then, half will halve the scount and set itself into 0. On the second loop, the program will find out that half is no longer greater than 0, so the program will move to the ELSE statement and set scount to 0. This will end the WHILE statement's looping and we have all the values of the variables 0. The problem here is that after the program sets half to 0, it jumps straight to the ELSE statement and sets scount to 0. Why is that?

Aucun commentaire:

Enregistrer un commentaire