IndexError:列表索引超出范围

时间:2016-03-17 09:50:42

标签: python

我可以帮忙搞清楚代码有什么问题吗? 当我运行它时,它给了我这个错误:

Traceback (most recent call last):
  File "ap_settings.py", line 19, in <module>
    AP_NUMBER = SETTINGS[TEST_SETTINGS_INDEX][0]
IndexError: list index out of range

这是文件ap_settings.py:

# Define variables
# SETTINGS is [ (AP_NUMBER, SAVE_RESULTS, SKIP) ]

TEST_SETTINGS_INDEX = 3

SETTINGS = [
            (0, 0, 0),
            ]

# Defining the fuzzing MAC address device
STA_MAC = "00:20:A6:61:2D:09"

# Defining the injection interface
IFACE   = "ath0"

##### BELOW VARIABLES SHOULD NOT BE TWEAKED BY THE USER

AP_NUMBER = SETTINGS[TEST_SETTINGS_INDEX][0]
SAVE_RESULTS = SETTINGS[TEST_SETTINGS_INDEX][1]
SKIP = SETTINGS[TEST_SETTINGS_INDEX][2]

# Defining fuzzing specific variables
AP = [
        ('kikoo', '00:11:22:33:44:55', 11, 'WPA-PSK'),
        ][AP_NUMBER]

SSID = AP[0]
AP_MAC = AP[1]
CHANNEL = chr(AP[2])
AP_CONFIG = AP[3]

# Defining the number of retries when authenticating/associating to the AP
CRASH_RETRIES = 10
DELAY = 1
STATE_WAIT_TIME = 2
DELAY_REBOOT = 10
LOG_LEVEL = 3
CRASH_THRESHOLD = 3
TRUNCATE = True

# Defining the log file
FNAME = [None, 'audits/ap-%s-%s.session' % (AP_MAC, AP_CONFIG)][SAVE_RESULTS]

2 个答案:

答案 0 :(得分:3)

在文件的顶部,你有这个:

TEST_SETTINGS_INDEX = 3
SETTINGS = [
            (0, 0, 0),
            ]

然后再往下走一点:

AP_NUMBER = SETTINGS[TEST_SETTINGS_INDEX][0]
SAVE_RESULTS = SETTINGS[TEST_SETTINGS_INDEX][1]
SKIP = SETTINGS[TEST_SETTINGS_INDEX][2]

您正在尝试访问3的索引位置SETTINGS,因为TEST_SETTINGS_INDEX设置为SETTINGS,但0只有一个项目,所以您应该正在查看索引AP_NUMBER = SETTINGS[0][0] SAVE_RESULTS = SETTINGS[0][1] SKIP = SETTINGS[0][2]

-(void)loadData{
    if (!_imageArray||_imageArray.count > 0) {
        _imageArray = [NSMutableArray array];
    }
    if (!_newsArray||_newsArray.count > 0) {
        _newsArray = [NSMutableArray array];
    }
    if (!_articleArray||_articleArray.count > 0) {
        _articleArray = [NSMutableArray array];
    }

答案 1 :(得分:0)

SETTINGS列表包含一个(0,0,0)的元组,所以基本上你的列表只包含一个元素。 即len(SETTINGS)=1

所以你的代码说

AP_NUMBER=SETTINGS[TEST_SETTINGS_INDEX][0]

其中TEST_SETTINGS_INDEX = 3,i:e

AP_NUMBER=SETTINGS[3][0]

这意味着您正在访问列表中不存在的第3个元素,这就是为什么您将错误&#39;索引超出范围&#39;。

此外,list包含n个元素,然后访问您需要使用n-1的第n个元素。 例如

 x = [1,2,3,4] then x[0]=1, x[1]=2, x[2]=3, x[3]=4

所以在你的情况下代码应该是

AP_NUMBER = SETTINGS[0][TEST_SETTINGS_INDEX]

此外,因为SETTINGS列表中的len元组是3 i:e

len((0,0,0)) = 3

因此,您的TEST_SETTINGS_INDEX值应介于0到2之间。

AP_NUMBER = SETTINGS[0][0]
AP_NUMBER = SETTINGS[0][1]
AP_NUMBER = SETTINGS[0][2]

有关详细信息,请参阅list and tuples