“NoneType"对象不可下标? [英] 'NoneType' object is not subscriptable?

查看:66
本文介绍了“NoneType"对象不可下标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

list1 = ["name1", "info1", 10]
list2 = ["name2", "info2", 30]
list3 = ["name3", "info3", 50]
MASTERLIST = [list1, list2, list3]


def printer(lst):
    print ("Available Lists:")
    for x in range(len(lst)):
        print (lst[x])[0]

此代码返回'NoneType' 对象不可下标";我尝试运行时出错

This code is returning the "'NoneType' object is not subscriptable" error when I try and run

printer(MASTERLIST)

我做错了什么?

推荐答案

print() 函数返回 None.您正在尝试索引 None.您不能,因为 'NoneType' 对象不可下标.

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

[0] 放在括号内.现在您正在打印所有内容,而不仅仅是第一学期.

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

这篇关于“NoneType"对象不可下标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆