为什么我一直得到这个IndexError?

时间:2017-03-04 06:44:34

标签: python python-2.7 index-error

我是python和编码的新手。任何人都可以理解为什么我得到这个IndexError?

canActivate(route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Promise<boolean> {

    let promise = new Promise((resolve, reject) => {

        this.af.auth.subscribe(user => {

            if (user) {
                let userProfile = firebase.database().ref(`/users/${user.uid}`);
                userProfile.once("value", (snapshot) => {
                    let userInfo = snapshot.val().role;
                    if (userInfo === "provider") {
                        resolve(true);
                    } else {
                        reject(false);
                    }
                });
            }
        });

    }).catch(() => {
        this.router.navigate(['/login'])
    });

    return promise.then(() => {
        return true;
    }, error => {
        console.log(error);
    })
 }
}

我试图从我的每个if语句/套件中提取的列表有四个项目,但由于某种原因,它一直告诉我列表索引超出范围。

Traceback (most recent call last):
  File "\\Mac\Home\Desktop\Python\Lab1\lab1_querystring.pageresults.addresssplit.headersadded.columnsadded.keysaddedtodictionary.py", line 54, in <module>
    city = splitaddress[1]
IndexError: list index out of range

1 个答案:

答案 0 :(得分:0)

您的地址不包含逗号,而是另一个分隔符:

In [1]: address = "Mr. Foo; Spam street 42; Eggsville"

In [2]: address.split(',')
Out[2]: ['Mr. Foo; Spam street 42; Eggsville']

如果您要将此内容分配给splitaddress,则splitaddress[0]以外的所有内容都会引发IndexError

在继续之前检查len(splitaddress) > 1