lundi 28 janvier 2019

Python : loop in if else statement in a loop

For each iteration in given script, i am importing a data and computing a variable. for simplicity, i am not including all that stuff and lets say i want to print the ID for each of 25 cases. In whatever way, i place the last statement, it always prints - either all iterations for last data or last iteration for all data.

for data in range(0,5):

if data==0:
    for iteration in range(0,5):
        # Import some data and compute some variables
        ID = (data,iteration)

elif data ==1:
    for iteration in range(0,5):
        # Import some data and compute some variables
        ID = (data,iteration)

elif data ==2:
    for iteration in range(0,5):
        # Import some data and compute some variables
        ID = (data,iteration)

elif data ==3:
    for iteration in range(0,5):
        # Import some data and compute some variables
        ID = (data,iteration)           

elif data ==4:
    for iteration in range(0,5):
        # Import some data and compute some variables
        ID = (data,iteration)


ComputedValue = data + iteration                                
print( 'ComputedValue = %.1d for ID = (%.1d,%.1d)' %(ComputedValue,data,iteration) )

Currently it prints only for 5 cases:

ComputedValue = 4 for ID = (0,4)
ComputedValue = 5 for ID = (1,4)
ComputedValue = 6 for ID = (2,4)
ComputedValue = 7 for ID = (3,4)
ComputedValue = 8 for ID = (4,4)

What changes/modifications to be done, so it prints for all 25 cases? Thanks!!

Rachit

Aucun commentaire:

Enregistrer un commentaire