Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对于带if语句的列表循环,只有列表的最后一个位置运行if语句_Python_Python 3.x - Fatal编程技术网

Python 对于带if语句的列表循环,只有列表的最后一个位置运行if语句

Python 对于带if语句的列表循环,只有列表的最后一个位置运行if语句,python,python-3.x,Python,Python 3.x,这是我的列表,所以模型[0]、名称[0]、mac_地址[0]都需要计算我需要的mac地址 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"] 这些函数应该根据哪个if语句为True来运行 def calc18(mac_address, name, mr):

这是我的列表,所以模型[0]、名称[0]、mac_地址[0]都需要计算我需要的mac地址

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"]
这些函数应该根据哪个if语句为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)
现在我有了这个for循环,它应该遍历模型中的每个位置,然后检查if语句并执行特定的函数

因此,第一个迭代模型[0]中存储了MR18,然后将检查if语句并执行calc18, calc18函数完成执行后,将返回for循环并运行下一个迭代,该迭代将是具有MR32的模型[1],然后再次检查if语句并执行calc32。然后,当calc32完成执行后,转到模型[2]

但在我的例子中,当我运行代码时,表示MR18的模型[2]通过if语句运行,完全忽略模型[0]和模型[1]


如果这仍然让人困惑,请让我知道,我不确定是否允许使用图片,如果允许的话,我可以画一个可视化示例:

由于您的代码在我的系统上无法运行,我运行了一个更简单的代码版本,以说明您的代码确实应该遍历模型的每个元素

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])
产生:

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
代码会遍历列表中的每个元素。问题不应该出在for循环上


我在您的代码中看到writer,您可能正在将其中的writer写入文件。请检查它是否设置为覆盖模式或附加模式

你所说的挑战是什么意思?好的,我想让for循环将模型[0]IF==检查为我在IF语句中设置的一个字符串,然后执行其中的代码。然后在它运行完所有代码后,转到模型[1]再次检查每个IF语句,然后模型[2]检查IF语句,同时更新标题我希望这是一种更正确的方式,说明如何从示例中生成最后的打印输出?它的代码是什么?对于rangelenmodels中的i:如果i是lenmodels-1:打印模型[i],名称[i],mac_地址[i]到其他网站的链接不受欢迎。请提供一份表格,谢谢。我基本上是从一个文本文件中接收我的列表名称、型号、mac\U地址,该文本文件将\n添加到列表中的项目中。这就是问题的根源。我忽略了这一点。多谢各位
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