错误: “ int”对象不可下标 -Python

我只是尝试了一个简单的代码,得到一个人的名字和年龄,让他/她知道,当他们到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."
790705 次浏览

问题出在队伍里,

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."

当您键入 x = 0时,它正在创建一个新的 int变量(名称)并为其分配一个零。

当您键入 x[age1]时,它将尝试访问 age1‘ th 条目,就像 x是一个数组一样。

当你写 x = 0时,x是一个 int... 所以你不能写 x[age1],因为 xint

你想在这里做什么: 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)

首先需要将 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."
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")

好吧,所有这些答案都是正确的,但这里有一个更现代的方法来做这件事!

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

‘ int’object is not subscriptable is TypeError in Python。为了更好地理解这个错误是如何发生的,让我们考虑下面的例子:

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

如果我们运行代码,您将在 Python 3中收到相同的 TypeError。

TypeError: 'int' object is not subscriptable

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

print(list1[0])

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

1

当代码(与问题一起给出)运行时,会出现 TypeError 并指向代码的第4行:

int([x[age1]])

其目的可能是创建一个整数数字的列表(尽管根本不需要为单个数字创建列表)。需要做的是将输入(转换为整数)赋给一个变量。

因此,最好这样编码:

name = input("What's your name? ")
age = int(input('How old are you? '))
twenty_one = 21 - age
if(twenty_one < 0):
print('Hi {0}, you are above 21 years' .format(name))
elif(twenty_one == 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, twenty_one))

输出:

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

这样做要简单得多;

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))`

X 已经是整数了(x = 0) ,你又一次尝试让 x 再次变成整数,你给出的索引超出了限制,因为 x 已经只有一个索引(0) ,你试图给出索引与年龄相同,这就是为什么你会得到这个错误。使用这个简单的代码

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.")