目录
◈介绍
In this article, we will be discussing certain type of error in 竞彩篮球分析. To be more specific we will be discussing the reason behind the occurrence of : TypeError:'int' Object Is Not Subscriptable
在 竞彩篮球分析 and the methods to overcome such errors.
让’我们来看一个示例,该示例演示了此类错误的发生。
例子: 考虑以下程序:
1 2 3 4 5 |
数 = 整型(输入(“输入3位数字吗?”)) sum_digit = 数 [0]+ 数 [1]+ 数 [2] 打印(sum_digit) |
输出:
1 2 3 4 5 6 |
追溯 ( 最多 最近的 称呼 最后的 ): 文件 “ D:/ PycharmProjects / pythonProject1 / TypeError Not Subscriptable.py”, 线 7, 在 <模块> sum_digit = 数 [0]+ 数 [1]+ 数 [2] TypeError: 'int' 目的 是 不是 可下标的 |
如果您遇到类似的错误/错误,那么一定很令人沮丧! 😩
但这也给我们带来了一系列问题:
- 不可下标对象是什么意思?
- 什么是
TypeError
? - 什么是
TypeError:'int'对象不可下标
? - 我该如何解决:
TypeError:'int'对象不可下标
?
因此,让我们不再拖延地发现问题的答案,然后解决问题。
Object对象不可下标是什么意思?
简单来说, 可下标的 目的 竞彩篮球分析中的对象是可以包含其他对象的对象,即作为容器的对象可以称为 可下标的对象。 Strings
, tuples
, lists
和 dictionaries
are examples of 可下标的 对象 在 竞彩篮球分析.
➟ 为什么整数不是下标对象?
整数是整数。它们中不能包含其他对象。进一步, 可下标对象 implement the __getitem__()
method and integer objects do not implement the __getitem__()
method.

➥什么是 TypeError In 竞彩篮球分析?
A TypeError
is raised when a certain operation is applied to an object of an incorrect type. For example, if you try to add a string object and an integer object using the +
operator, then you will encounter a TypeError
because the +
operation is not allowed between the two objects that are f different types.
例子:
1 2 3 |
打印('Java'+2+'博客') |
输出:
1 2 3 4 |
TypeError: 能够 只要 级联 力量 ( 不是 "整型") 到 力量 |
➥什么是: TypeError:’int’对象不可下标?
- 你会遇到
TypeError:object is not subscriptable
在竞彩篮球分析中,当您尝试对不可下标的对象使用索引时。 - Since integer is not a subscriptable object, thus if you try to use indexing upon an integer object then 竞彩篮球分析 will throw the following error:
TypeError:'int'对象不可下标
.
That brings us to our next questions:- What are some of the scenarios where we come across TypeError:'int'对象不可下标
和 how can we fix it?
To answer the above questions, let us visualize the occurrence and solution to this kind of TypeError
和 help of examples.
✨方案1:尝试访问整数对象的索引
我们已经在本文的简介部分讨论了问题说明,在这里我们试图找到一个三位数的数字的总和。
However, we got TypeError:object is not subscriptable
在 our futile attempt to derive the sum. The reason was: we treated the integer object 数
as a container object and tried to access it using its indices.
现在它’是时候进行修改,并使我们的计划生效!😃
解决方案:
✯方法1:将整数对象转换为字符串对象
一个简单的解决方案是:
- accept the user input
数
as a string, - 现在,每个数字都是字符串,因此可以使用它们的索引来访问它们。将每个数字字符串转换为整数,然后计算总和。
1 2 3 4 5 |
数 = 输入(“输入3位数字吗?”) sum_digit = (整型( 数 [0]) + 整型( 数 [1]) + 整型( 数 [2])) 打印(sum_digit) |
输出:
1 2 3 4 |
进入 a 3 数字 数字? 754 16 |
✯方法2:覆盖__getitem__方法
Another approach to solving the non-subscriptable TypeError
是 to redefine the __getitem__
method in the code itself as shown below:
1 2 3 4 5 6 7 8 9 10 |
班级 潜艇: 定义 __getitem__( 自己 , 物品): 返回 整型(物品) 对象 = 潜艇() 数 = 输入(“输入3位数字吗?”) 打印( 对象 [ 数 [0]] + 对象 [ 数 [1]] + 对象 [ 数 [2]]) |
输出:
1 2 3 4 |
进入 a 3 数字 数字? 546 15 |
解释:
在这种情况下,我们在代码中定义了__getitem__方法,并使其以整数形式返回三位数字的每个数字。
✨方案2:将整数视为列表
下面给出的是另一种情况– TypeError:'int'对象不可下标
.
1 2 3 4 5 6 7 8 |
物品 = 输入(“输入商品名称:”) 价格 = 输入(“输入商品价格:”) x = 0 整型(x[价格]) 打折后价格 = x-3000 打印 (“一个的价格”,物品, “是卢比。”,打折后价格) |
输出:
1 2 3 4 5 6 7 8 9 10 |
进入 物品 姓名 : 苹果手机 12 进入 物品 价格 : 79900 追溯 ( 最多 最近的 称呼 最后的 ): 文件 “ D:/ PycharmProjects / pythonProject1 / TypeError Not Subscriptable.py”, 线 4, 在 <模块> 整型(x[价格]) TypeError: 'int' 目的 是 不是 可下标的 过程 完成的 和 出口 代码 1 |
In the above program, 价格
是 an integer value, however we tried to use it as a list by using its index. Thus we got the error!
解决方案:
在这种情况下,解决方案非常简单。您只需要避免将整数对象用作容器类型对象。
1 2 3 4 5 6 7 8 |
物品 = 输入(“输入商品名称:”) 价格 = 输入(“输入商品价格:”) x = (整型(价格)) 打折后价格 = x - 3000 打印(“一个的价格”, 物品, “是卢比。”, 打折后价格) |
输出:
1 2 3 4 5 6 7 |
进入 物品 姓名 : 苹果手机 12 进入 物品 价格 : 79900 价格 的 an 苹果手机 12 是 Rs . 76900 过程 完成的 和 出口 代码 0 |
结论
We learned some key points about how to deal with TypeError:'int'对象不可下标
在竞彩篮球分析中。 To avoid these errors in your code, remember:
竞彩篮球分析加薪 TypeError:object is not subscriptable
如果您使用索引,即方括号表示法是带有不可下标的对象。要解决此问题,您可以:
- 将不可下标的对象包装为字符串,列表,元组或字典的容器数据类型,或者
- 通过删除索引调用,或者
- by defining the
__getitem__
method in your code.
希望本文对您有所帮助!请 订阅 和 敬请关注 有关将来的更多文章。学习愉快!📚