python: "TypeError: 'type' object is not subscriptable"
目前stackoverflow找到两种情况的解决办法:
1、TypeError: 'type' object is not subscriptable when indexing in to a dictionary
|
I have multiple files that I need to load so I'm using a
|
Normally Python throws NameError if the variable is not defined:
>>> d[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'd' is not defined
However, you've managed to stumble upon a name that already exists in Python.
Because dict is the name of a built-in type in Python you are seeing what appears to be a strange error message, but in reality it is not.
The type of dict is a type. All types are objects in Python. Thus you are actually trying to index into the type object. This is why the error message says that the "'type' object is not subscriptable."
>>> type(dict)
<type 'type'>
>>> dict[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'type' object is not subscriptable
Note that you can blindly assign to the dict name, but you really don't want to do that. It's just going to cause you problems later.
>>> dict = {1:'a'}
>>> type(dict)
<class 'dict'>
>>> dict[1]
'a'
The true source of the problem is that you must assign variables prior to trying to use them. If you simply reorder the statements of your question, it will almost certainly work:
d = {1: "walk1.png", 2: "walk2.png", 3: "walk3.png"}
m1 = pygame.image.load(d[1])
m2 = pygame.image.load(d[2])
m3 = pygame.image.load(d[3])
playerxy = (375,130)
window.blit(m1, (playerxy))
2、TypeError: 'int' object is not subscriptable
I'm trying to create a simple program that tells you your lucky number according to numerology. I keep on getting this error:
File "number.py", line 12, in <module>
sumln = (int(sumall[0])+int(sumall[1]))
TypeError: 'int' object is not subscriptable
My script is:
birthday = raw_input("When is your birthday(mm/dd/yyyy)? ")
summ = (int(birthday[0])+int(birthday[1]))
sumd = (int(birthday[3])+int(birthday[4]))
sumy= (int(birthday[6])+int(birthday[7])+int(birthday[8])+int(birthday[9]))
sumall = summ + sumd + sumy
print "The sum of your numbers is", sumall
sumln = (int(sumall[0])+int(sumall[1]))
print "Your lucky number is", sumln`
If you want to sum the digit of a number, one way to do it is using sum() + a generator expression:
sum(int(i) for i in str(155))
I modified a little your code using sum(), maybe you want to take a look at it:
birthday = raw_input("When is your birthday(mm/dd/yyyy)? ")
summ = sum(int(i) for i in birthday[0:2])
sumd = sum(int(i) for i in birthday[3:5])
sumy = sum(int(i) for i in birthday[6:10])
sumall = summ + sumd + sumy
print "The sum of your numbers is", sumall
sumln = sum(int(c) for c in str(sumall)))
print "Your lucky number is", sumln
python: "TypeError: 'type' object is not subscriptable"的更多相关文章
- Debug 路漫漫-11:Python: TypeError: 'generator' object is not subscriptable
调试程序,出现以下错误: Python: TypeError: 'generator' object is not subscriptable “在Python中,这种一边循环一边计算的机制,称为生成 ...
- python报错:TypeError: 'int' object is not subscriptable
检查一遍报错的所在行,此报错一般是在整数上加了下标: 比如: a = 4 c=a[2] 报错:line 2, in <module> c=a[2] TypeError: 'i ...
- openpyxl使用sheet.rows或sheet.columns报TypeError: 'generator' object is not subscriptable解决方式
解决方案: 因为新版本的openpyxl使用rows或者columns返回一个生成器所以可以使用List来解决报错问题 >>> sheet.columns[0] Traceback ...
- TypeError: 'NoneType' object is not subscriptable
运行,显示TypeError: 'NoneType' object is not subscriptable错误信息,原因是变量使用了系统内置的关键字list 重新定义下这个变量就好了
- openpyxl中遇到TypeError: 'generator' object is not subscriptable的问题和解决方案
今天在搭建驱动数据框架用到了一个叫 openpyxl的包用来解析excel数据 随后就出现了TypeError: 'generator' object is not subscriptable的bug ...
- TypeError: 'generator' object is not subscriptable
TypeError: 'generator' object is not subscriptable 生成器对象不可以带下标 def get_row(self,row_no): if not isin ...
- Python TypeError: 'module' object is not callable 原因分析
今天尝试使用pprint进行输出,语句为 >>>import pprint >>>pprint(people) 结果报错,TypeError: 'module' o ...
- Python: TypeError: 'dict' object is not callable
问题: TypeError: 'dict' object is not callable 原因: dict()是python的一个内建函数,如果将dict自定义为一个python字典,在之后想调用 ...
- python -- TypeError: 'module' object is not callable
文件: 代码: import pprintmessge = 'It was a bringht cold day in April,and the clocks were striking thrir ...
随机推荐
- 吴裕雄 13-MySQL UPDATE 查询
以下是 UPDATE 命令修改 MySQL 数据表数据的通用 SQL 语法:UPDATE table_name SET field1=new-value1, field2=new-value2[WHE ...
- reids遇到问题
今天重启爬虫服务器在连接redis数据库时突然报错:MISCONF Redis is configured to save RDB snapshots, but it is currently not ...
- python闭包的代码
- ADO.Net 数据库 删除
删除数据库里的信息和之前增加,修改大同小异,其写法更加简单,也是把SQL语句写为删除语句 删除一条数据,只需要获取并接收到这条数据唯一的能够代表这条数据的信息,比如主键 代码演示: using Sys ...
- apache安装配置
因为个人是在docker上面做实验的,所以可以多少会有些出入. 1.先启动一个docker,配置好基本的工具,网络啊,ssh啊是,tar啊,wget啊,vim等等. 其次去官网获取自己想要的压缩文件的 ...
- 1.3.6、CDH 搭建Hadoop在安装之前(端口---DistCp使用的端口)
DistCp使用的端口 列出的所有端口都是TCP. 在下表中,每个端口的“ 访问要求”列通常是“内部”或“外部”.在此上下文中,“内部”表示端口仅用于组件之间的通信; “外部”表示该端口可用于内部或外 ...
- app.route()
[app.route()] 可使用 app.route() 创建路由路径的链式路由句柄.由于路径在一个地方指定,这样做有助于创建模块化的路由,而且减少了代码冗余和拼写错误.请参考 Router() 文 ...
- pytest 学习笔记二:兼容unittest、执行方式、生成报告
1.官方文档上说pytest兼容unittest时,不支持setUpModule 和 tearDownModule,但实际验证是可以的. 验证的场景是py文件中,只有一个测试类, 经验证有多个测试类, ...
- Cisco N3K VPC+HSRP+ospf 配置
VPC概念 VPC:vpc是指vpc对等体设备和下游设备之间的组合PortChannel. vpc对等交换:就是组成vpc功能的两个nexus系列交换机,一个设备为主,一个为备. vpc对等连接:用于 ...
- Git 远程仓库 更新url
git remote -v 查看现有远程仓库的地址url 1. 修改命令git remote set-url origin <URL> 更换远程仓库地址.把<URL>更换为新的 ...