mercredi 5 septembre 2018

Putting boundaries and transforming variables (bolean python)

I am at my beginning journey of learning python and i am struggling to get my head around transforming entries in a code.

I have a for loop going and for every loop i create 4 variables.So below is the result of four loops

A    B   C    D
0    0   18   19
0    0   52   52
0    0   18   19
-1  -1   19   52
-1  -1   19   20

Expected result:

A    B   C    D
0    0   18   19
0    0   50   50 (## just a mark no need to be here just to point)
0    0   18   19
0    0   19   52
0    0   19   20

So i though this would work, what i need if that none of this variables to be greater than 50 or below 0, if they are below zero they must become 0 and if they are above 50 then 50 , else just the variable itself:

if A < 0:
     A = 0
elif A>50:
     A = 50
elif 0<A<50:
     A=A
elif if A < 0:
     A = 0
elif B>50:
     B = 50
elif 0<B<50:
     B=B    ......

I also though about defining a function and passing the 4 variables each time to the function..

Aucun commentaire:

Enregistrer un commentaire