Home > Software design >  python for loop and if not working properly
python for loop and if not working properly

Time:05-30

I have below code and it seems not working properly.

cylinderCount = [8,5,5,8]
engineCount = 4

for no in range(engineCount):
    for i in range(cylinderCount[no]):
       engineNo = str(no 1)

    if cylinderCount == 8:
       col = ["GA022" str(i)   '_0' str(engineNo) for i in range(20, cylinderCount[no]*10 11,10)]   ['GA02291' '_0' str(engineNo)]
    else:
       col = ["GA022" str(i)   '_0' str(engineNo) for i in range(20, cylinderCount[no]*10 11,10)] 

The col should be

GA02220_04,GA02230_04,GA02240_04,GA02250_04,GA02260_04,GA02270_04,GA02280_04,GA02290_04,GA02291_04

but some how I only get

GA02220_04,GA02230_04,GA02240_04,GA02250_04,GA02260_04,GA02270_04,GA02280_04,GA02290_04

Can someone please tell me what I not getting GA02291_04?

CodePudding user response:

you defined cylinderCount as a list, and then try to compare with a number, try changing.

...
if cylinderCount == 8:
....

to

...
if cylinderCount[no] == 8:
...

CodePudding user response:

Your code example is big hard to digest, since you assign col but don't use it. This does the exact same thing as your code, but then cleaned-up and more DRY. Small assumption since your indentation is a bit off.

cylinderCount = [8, 5, 5, 8]
engineCount = 4

rows = []
for engineno_int, cylinders in enumerate(cylinderCount):
    engineNo = str(engineno_int   1)

    for i in range(cylinders):

        col = [f"GA022{i}_0{engineNo}" for i in range(20, cylinders * 10   11, 10)]
        if cylinders == 8:
            col  = [f"GA02291_0{engineNo}"]
        rows.append(col)

Printing instead of append the col result in this:

['GA02220_01', 'GA02230_01', 'GA02240_01', 'GA02250_01', 'GA02260_01', 'GA02270_01', 'GA02280_01', 'GA02290_01', 'GA02291_01']
['GA02220_01', 'GA02230_01', 'GA02240_01', 'GA02250_01', 'GA02260_01', 'GA02270_01', 'GA02280_01', 'GA02290_01', 'GA02291_01']
['GA02220_01', 'GA02230_01', 'GA02240_01', 'GA02250_01', 'GA02260_01', 'GA02270_01', 'GA02280_01', 'GA02290_01', 'GA02291_01']
['GA02220_01', 'GA02230_01', 'GA02240_01', 'GA02250_01', 'GA02260_01', 'GA02270_01', 'GA02280_01', 'GA02290_01', 'GA02291_01']
['GA02220_01', 'GA02230_01', 'GA02240_01', 'GA02250_01', 'GA02260_01', 'GA02270_01', 'GA02280_01', 'GA02290_01', 'GA02291_01']
['GA02220_01', 'GA02230_01', 'GA02240_01', 'GA02250_01', 'GA02260_01', 'GA02270_01', 'GA02280_01', 'GA02290_01', 'GA02291_01']
['GA02220_01', 'GA02230_01', 'GA02240_01', 'GA02250_01', 'GA02260_01', 'GA02270_01', 'GA02280_01', 'GA02290_01', 'GA02291_01']
['GA02220_01', 'GA02230_01', 'GA02240_01', 'GA02250_01', 'GA02260_01', 'GA02270_01', 'GA02280_01', 'GA02290_01', 'GA02291_01']
['GA02220_02', 'GA02230_02', 'GA02240_02', 'GA02250_02', 'GA02260_02']
['GA02220_02', 'GA02230_02', 'GA02240_02', 'GA02250_02', 'GA02260_02']
['GA02220_02', 'GA02230_02', 'GA02240_02', 'GA02250_02', 'GA02260_02']
['GA02220_02', 'GA02230_02', 'GA02240_02', 'GA02250_02', 'GA02260_02']
['GA02220_02', 'GA02230_02', 'GA02240_02', 'GA02250_02', 'GA02260_02']
['GA02220_03', 'GA02230_03', 'GA02240_03', 'GA02250_03', 'GA02260_03']
['GA02220_03', 'GA02230_03', 'GA02240_03', 'GA02250_03', 'GA02260_03']
['GA02220_03', 'GA02230_03', 'GA02240_03', 'GA02250_03', 'GA02260_03']
['GA02220_03', 'GA02230_03', 'GA02240_03', 'GA02250_03', 'GA02260_03']
['GA02220_03', 'GA02230_03', 'GA02240_03', 'GA02250_03', 'GA02260_03']
['GA02220_04', 'GA02230_04', 'GA02240_04', 'GA02250_04', 'GA02260_04', 'GA02270_04', 'GA02280_04', 'GA02290_04', 'GA02291_04']
['GA02220_04', 'GA02230_04', 'GA02240_04', 'GA02250_04', 'GA02260_04', 'GA02270_04', 'GA02280_04', 'GA02290_04', 'GA02291_04']
['GA02220_04', 'GA02230_04', 'GA02240_04', 'GA02250_04', 'GA02260_04', 'GA02270_04', 'GA02280_04', 'GA02290_04', 'GA02291_04']
['GA02220_04', 'GA02230_04', 'GA02240_04', 'GA02250_04', 'GA02260_04', 'GA02270_04', 'GA02280_04', 'GA02290_04', 'GA02291_04']
['GA02220_04', 'GA02230_04', 'GA02240_04', 'GA02250_04', 'GA02260_04', 'GA02270_04', 'GA02280_04', 'GA02290_04', 'GA02291_04']
['GA02220_04', 'GA02230_04', 'GA02240_04', 'GA02250_04', 'GA02260_04', 'GA02270_04', 'GA02280_04', 'GA02290_04', 'GA02291_04']
['GA02220_04', 'GA02230_04', 'GA02240_04', 'GA02250_04', 'GA02260_04', 'GA02270_04', 'GA02280_04', 'GA02290_04', 'GA02291_04']
['GA02220_04', 'GA02230_04', 'GA02240_04', 'GA02250_04', 'GA02260_04', 'GA02270_04', 'GA02280_04', 'GA02290_04', 'GA02291_04']

CodePudding user response:

You compare cylinderCount with 8. Since cylinderCount is defined as [8,5,5,8] this will never be true. Maybe you meant cylinderCount[no]. In this case the program is overcomplicated and could be written as :

cylinders = [8, 5, 5, 8]
for no, cylinder in enumerate(cylinders, start=1):
    col = [f'GA022{i}_0{no}' for i in range(20, cylinder * 10   11, 10)]
    if cylinder == 8:
        col  = [f'GA02291_0{no}']
    print(col)

With the result:

['GA02220_01', 'GA02230_01', 'GA02240_01', 'GA02250_01', 'GA02260_01', 'GA02270_01', 'GA02280_01', 'GA02290_01', 'GA02291_01']
['GA02220_02', 'GA02230_02', 'GA02240_02', 'GA02250_02', 'GA02260_02']
['GA02220_03', 'GA02230_03', 'GA02240_03', 'GA02250_03', 'GA02260_03']
['GA02220_04', 'GA02230_04', 'GA02240_04', 'GA02250_04', 'GA02260_04', 'GA02270_04', 'GA02280_04', 'GA02290_04', 'GA02291_04']
  • Related