david yeritsyan
david yeritsyan

Reputation: 452

for-loop through a list with an if statement, only last position of list runs if statements

Here is my list, So models [0], names[0], mac_addresses[0] would all be required to calculate my mac address that I need

models = ["MR18","MR32", "MR18"]
names = ["David", "Bob", "Frank"]
mac_addresses = ["00:18:0A:2C:3D:5F", "00:18:0A:2d:3c:5F", "00:18:0A:2A:3B:5F"]

These are the functions that should run depending on which if statement is True.

def calc18(mac_address, name, mr):
    #Mac Address Breakdown
    print(name)
    mac_calc = mac_address[:2]
    mac_extractor_front = mac_address[2:6]
    mac_extractor_back = mac_address[8:]

    flag = True

    First_Pos_Hex = first_hex_calc(mac_calc, mr, flag)

    #Initial Mac Addresses
    list_2_4.append(mac_address)
    list_5.append(First_Pos_Hex + mac_extractor_front + mr_18_5ghz + mac_extractor_back)

    flag = False
    #First Time Calculation hex updated
    hex_updater = first_hex_calc(mac_calc, mr, flag)

    list_2_4.append(hex_updater + mac_extractor_front + mr_18_24ghz + mac_extractor_back)
    list_5.append(hex_updater + mac_extractor_front + mr_18_5ghz + mac_extractor_back)

    #Update self, after appending mac addresses    
    for i in range(15):
        counter = i + 1
        hex_updater = hex_calc(hex_updater, mr) 
        list_2_4.append(hex_updater + mac_extractor_front + mr_18_24ghz + mac_extractor_back)
        list_5.append(hex_updater + mac_extractor_front + mr_18_5ghz + mac_extractor_back)
        print(str(counter) + ") 2.4ghz: " + list_2_4[i] + "\t" + str(counter) + ") 5 Ghz: " + list_5[i] )

        for i in range(len(list_2_4)):
            writer(name, mac_address, list_2_4[i], list_5[i], i)


def calc32(mac_address, name):
    #Mac Address Breakdown
    mac_calc = mac_address[15:17]
    mac_extractor_front = mac_address[:6]
    mac_extractor_back = mac_address[8:15]


    #Initial Mac Addresses
    list_2_4.append(mac_extractor_front + mr_32_24ghz + mac_extractor_back + mac_calc)
    list_5.append(mac_extractor_front + mr_32_5ghz + mac_extractor_back + mac_calc)


    #Update self, after appending mac addresses    
    for i in range(15):
        counter = i + 1
        mac_calc = hex_calc(mac_calc, mr) 
        list_2_4.append(mac_extractor_front + mr_32_24ghz + mac_extractor_back + mac_calc)
        list_5.append(mac_extractor_front + mr_32_5ghz + mac_extractor_back + mac_calc)
        print(str(counter) + ") 2.4ghz: " + list_2_4[i] + "\t" + str(counter) + ") 5 Ghz: " + list_5[i] )
        writer(name, mac_address, list_2_4[i], list_5[i], i)

Now I have this for-loop should iterate through each position in models, which would then check the if statements and execute a specific function

so, the first iteration models[0] which has "MR18" stored inside, then would check the if statements and execute calc18(), after calc18() function finishes its execution would return to the for-loop and run the next iteration which would be models[1] which has "MR32", then again would check the if statements and execute calc32(). Then when calc32() finishes executing move on to models[2]

But in my case when I run the code, models[2] which represents "MR18" runs through the if statement, completely ignoring models[0] and models[1]

for num, mod in enumerate(models):
    print(mod)
    if mod == "MR18":
        print("I have entered")
        calc18(mac_addresses[num], names[num], mod)
    if mod == "MR32":
        print("I have entered 32")
        calc32(mac_addresses[num], names[num])

If this is still confusing please let me know, I'm not sure if pictures are allowed, I can draw a visual example if that's allowed :(

Upvotes: 0

Views: 66

Answers (1)

Siong Thye Goh
Siong Thye Goh

Reputation: 3586

As your code is not runnable on my system, I run a simpler version of code to illustrate that your code should indeed go through each element of models.

models = ["MR18","MR32", "MR18"]
names = ["David", "Bob", "Frank"]
mac_addresses = ["00:18:0A:2C:3D:5F", "00:18:0A:2d:3c:5F", "00:18:0A:2A:3B:5F"]

def calc18(mac_address, name, mr):
    print("calc18 running")
    print(mac_address)
    print(name)
    print(mr)
def calc32(mac_address, name):
    print("calc32 running")
    print(mac_address)
    print(name)

for num, mod in enumerate(models):
    print(num)
    print(mod)
    if mod == "MR18":
        print("I have entered")
        calc18(mac_addresses[num], names[num], mod)
    if mod == "MR32":
        print("I have entered 32")
        calc32(mac_addresses[num], names[num])

produces:

0
MR18
I have entered
calc18 running
00:18:0A:2C:3D:5F
David
MR18
1
MR32
I have entered 32
calc32 running
00:18:0A:2d:3c:5F
Bob
2
MR18
I have entered
calc18 running
00:18:0A:2A:3B:5F
Frank
MR18

The code does go through each element of the list. The problem shouldn't be with the for loop.

I see writer in your code, of which you might be writing to a file. Do check if it is set to overwrite mode or append mode.

Upvotes: 1

Related Questions