'NoneType' object is not subscriptable?

前端 未结 6 1349
深忆病人
深忆病人 2020-12-05 18:46
list1 = ["name1", "info1", 10]
list2 = ["name2", "info2", 30]
list3 = ["name3", "info3", 50]
MASTERLIST =         


        
相关标签:
6条回答
  • 2020-12-05 19:07

    Don't use list as a variable name for it shadows the builtin.

    And there is no need to determine the length of the list. Just iterate over it.

    def printer(data):
        for element in data:
            print(element[0])
    

    Just an addendum: Looking at the contents of the inner lists I think they might be the wrong data structure. It looks like you want to use a dictionary instead.

    0 讨论(0)
  • 2020-12-05 19:12

    The indexing e.g. [0] should occour inside of the print...

    0 讨论(0)
  • 2020-12-05 19:14

    Point A: Don't use list as a variable name Point B: You don't need the [0] just

    print(list[x])
    
    0 讨论(0)
  • 2020-12-05 19:18

    The [0] needs to be inside the ).

    0 讨论(0)
  • 2020-12-05 19:19

    The print() function returns None. You are trying to index None. You can not, because 'NoneType' object is not subscriptable.

    Put the [0] inside the brackets. Now you're printing everything, and not just the first term.

    0 讨论(0)
  • 2020-12-05 19:30
    list1 = ["name1", "info1", 10]
    list2 = ["name2", "info2", 30]
    list3 = ["name3", "info3", 50]
    
    def printer(*lists):
        for _list in lists:
            for ele in _list:
                print(ele, end = ", ")
            print()
    
    printer(list1, list2, list3)
    
    0 讨论(0)
提交回复
热议问题