为什么12345不在列表中?它是列表中的第一个

时间:2014-12-04 05:18:11

标签: python

names=[["Scott","Jones","12345", 9],["Ray","Raymond","67890",8],["Chris","Page","45678", 2],["Chris","Davis","678",10],["Sally","Smoot","90123",5],["Lukas","Flipinski","78901",6],["Anit","Boddu","34567",7],["Janice","Joplin","56789",4]]

search=raw_input("Enter ID here: ")

for i in names:

    if search in i:
        index=names.index(search)

    if names[index][2]<5:
        print "Issue this student a warning"
    else:
        print "Issue this student a detention"

为什么所有号码都不在列表中

2 个答案:

答案 0 :(得分:3)

if search in i:
    index=names.index(search)
          ^^^^^

if search in i:
    index=i.index(search)

应该{​​{1}}而不是i

编辑:如果您想找到names使用

的索引
names

答案 1 :(得分:0)

而不是

if search in i:
    index=names.index(search)

你想要吗?

if search in i:
    index=i.index(search)

此外,您还有一些其他问题,例如,如果找不到index,那该怎么办 - 但这就是您的业务逻辑。