错误:' int' object不是可下载的 - Python

时间:2011-11-22 01:01:34

标签: python python-2.7 type-conversion

我正在尝试一段简单的代码,获取某人的姓名和年龄,并让他/她知道他们何时年满21岁......不考虑否定和所有这些,只是随机。

我不断收到此'int' object is not subscriptable错误。

name1 = raw_input("What's your name? ")
age1 = raw_input ("how old are you? ")
x = 0
int([x[age1]])
twentyone = 21 - x
print "Hi, " + name1+ " you will be 21 in: " + twentyone + " years."

10 个答案:

答案 0 :(得分:52)

当您键入正在创建新x = 0变量(名称)并为其指定零的int时。

当您输入试图访问x[age1]条目的age1时,就好像x是一个数组一样。

答案 1 :(得分:28)

问题在于,

int([x[age1]])

你想要的是

x = int(age1)

您还需要将int转换为输出的字符串...

print "Hi, " + name1+ " you will be 21 in: " + str(twentyone) + " years."

完整的脚本如下,

name1 = raw_input("What's your name? ")
age1 = raw_input ("how old are you? ")
x = 0
x = int(age1)
twentyone = 21 - x
print "Hi, " + name1+ " you will be 21 in: " + str(twentyone) + " years."

答案 2 :(得分:8)

当您撰写x = 0时,x是一个整数...因此您无法x[age1],因为xint

答案 3 :(得分:1)

你想在这做什么:int([x[age1]]) ??这毫无意义。

您只需将年龄输入转换为int

name1 = raw_input("What's your name? ")
age1 = raw_input ("how old are you? ")
twentyone = 21 - int(age1)
print "Hi, %s you will be 21 in: %d years." % (name1, twentyone)

答案 4 :(得分:1)

这样做会简单得多;

name = input("What's your name? ")
age = int(input("How old are you? "))
print ("Hi,{0} you will be 21 in {1} years.".format(name, 21 - age))`

答案 5 :(得分:0)

您需要先将age1转换为int,因此可以执行减号。之后将结果返回到字符串以显示:

name1 = raw_input("What's your name? ")
age1 = raw_input ("how old are you? ")
twentyone = str(21 - int(age1))
print "Hi, " + name1+ " you will be 21 in: " + twentyone + " years."

答案 6 :(得分:0)

name1 = input("What's your name? ")
age1 = int(input ("how old are you? "))
twentyone = str(21 - int(age1))

if age1<21:
    print ("Hi, " + name1+ " you will be 21 in: " + twentyone + " years.")

else:
    print("You are over the age of 21")

答案 7 :(得分:0)

嗯,所有这些答案都是正确的,但这是一种更现代的方法!

router.post('/order', auth.check.bind(auth), (req, res) => {
const paramsProdOrderId = [req.body.product_id, req.body.product_ordered];
db.query(sqlProdOrder, paramsProdOrderId).then((results) => {
    if (results.rowCount === 0) {
        res.redirect('/products');
        return;
    }
    res.redirect('/');
})

.catch((err) => {
    res.redirect('/error/404')
});

答案 8 :(得分:0)

在Python中,

'int'对象不可下标是TypeError。为了更好地理解此错误的发生方式,让我们考虑以下示例:

list1 = [1, 2, 3]
print(list1[0][0])

如果运行代码,您将在Python3中收到相同的TypeError。

TypeError: 'int' object is not subscriptable

此处列表的索引超出范围。如果将代码修改为:

print(list1[0])

现在列表的索引在范围内,输出将为1(因为Python列表中的索引从零开始)。

1

运行代码(与问题一起提供)时,发生TypeError并指向代码的第4行:

int([x[age1]])

其目的可能是创建一个整数列表(尽管完全不需要为单个数字创建列表)。只需将输入(然后转换为整数)分配给变量即可。

因此,最好以这种方式进行编码:

name = input("What's your name? ")
age = int(input('How old are you? '))
twentyOne = 21 - age
if(twentyOne < 0):
    print('Hi {0}, you are above 21 years' .format(name))
elif(twentyOne == 0):
    print('Hi {0}, you are 21 years old' .format(name))
else:
    print('Hi {0}, you will be 21 years in {1} year(s)' .format(name, twentyOne))

输出:

What's your name? Steve
How old are you? 21
Hi Steve, you are 21 years old

答案 9 :(得分:0)

x已经是整数(x = 0),并且您再次尝试使x再次成为整数,并且您还给出了索引限制,因为x已经只有一个索引(0),并且您尝试给与x相同的索引年龄,这就是为什么您会收到此错误。用这个简单的代码

name1 = input("What's your name? ")
age1 = int(input ("how old are you?" ))
x=0
twentyone = str(21-age1)
print("Hi, " +name1+ " you will be 21 in: " + twentyone + " years.")