List 为什么n不是整数?

List 为什么n不是整数?,list,typeerror,List,Typeerror,这会产生一个错误: TypeError:列表索引必须是整数,而不是str 我不明白。请帮助。您正在迭代字符串列表months,因此n已经是一个字符串。在for循环的每次迭代中,n等于months中的下一项 months=["Jan","Feb","March","April"] temp =[] for n in months: temp = int(input("please enter a number for ",months[n])) 您能添加这是哪种语言吗?因为您在中使用的

这会产生一个错误:

TypeError:列表索引必须是整数,而不是str


我不明白。请帮助。

您正在迭代字符串列表
months
,因此
n
已经是一个字符串。在
for
循环的每次迭代中,
n
等于
months
中的下一项

months=["Jan","Feb","March","April"]
temp =[]

for n in months:
    temp = int(input("please enter a number for ",months[n]))

您能添加这是哪种语言吗?因为您在中使用的是
,并且您正在迭代字符串数组,所以n是字符串,而不是整数。如果这是Python,然后你需要回到一个好的教程,重新阅读。你知道一个原因吗?这里的人都不能回答。这里的人很有帮助类型错误:输入最多需要1个参数,得到2个?
input
需要一个参数,但你给了它两个。如我的示例所示,您可以将
month
(一个字符串)连接到另一个字符串“please enter a number for”以获得最后一个字符串,该字符串作为一个参数发送到
input
@AlanRobinson try this-system.out.println(“请为“,+month”输入一个数字)
months=["Jan","Feb","March","April"]
temp =[]

for month in months:
    temp.append(int(input("please enter a number for " + month)))
 @AlanRobinson upto my knowledge it should not be  



temp = int(input("please enter a number for ",month)) 
as you r trying to type cast an array of type string to int but in unmanered way