TypeError:“int”对象不支持项分配-Python新增

TypeError:“int”对象不支持项分配-Python新增,python,Python,我试图在书中回答一些问题,但我还是被我的数组卡住了。我得到这个错误: count[i] = x TypeError: 'int' object does not support item assignment 我的代码: import pylab count = 0 for i in range (0,6): x = 180 while x < 300: count[i] = x x = x + 20 print coun

我试图在书中回答一些问题,但我还是被我的数组卡住了。我得到这个错误:

count[i] = x
TypeError: 'int' object does not support item assignment
我的代码:

import pylab
count = 0

for i in range (0,6):
    x = 180
    while x < 300:
        count[i] = x
        x = x + 20
        print count
基本上,我需要使用一个循环来存储一个数组中的值,直到它们达到300为止。我不知道我为什么会犯这样的错误

我可能说得不好。我需要使用一个循环来生成6个值,从200,220,240。。。设置为300,并将其存储在数组中。

您已将计数定义为整数,并且错误显示“int”对象不支持项分配,因此您需要一个列表:

count = []
并使用append函数将数字追加到列表中:

for i in range (0,6):
    x = 180
    while x < 300:
        count.append(x)
        x = x + 20
        print count
您已将计数定义为整数,并且错误显示“int”对象不支持项分配,因此您需要一个列表:

count = []
并使用append函数将数字追加到列表中:

for i in range (0,6):
    x = 180
    while x < 300:
        count.append(x)
        x = x + 20
        print count
count是一个整数。整数没有索引,只有基于它们的列表、元组或对象有索引

要使用列表,然后向其附加值:

count = []
for i in range (0,6):
    x = 180
    while x < 300:
        count.append(x)
        x = x + 20
        print count
count是一个整数。整数没有索引,只有基于它们的列表、元组或对象有索引

要使用列表,然后向其附加值:

count = []
for i in range (0,6):
    x = 180
    while x < 300:
        count.append(x)
        x = x + 20
        print count
使用字典

count = {}

for i in range (0,6):
    x = 180
    while x < 300:
        count[i] = x
        x = x + 20
        print count
使用字典

count = {}

for i in range (0,6):
    x = 180
    while x < 300:
        count[i] = x
        x = x + 20
        print count

您不需要while循环,您可以使用带有开始、停止和步骤的范围:

count = []
for i in xrange (6):
    for x in  xrange(180,300,20):
        count.append(x)
        print count
或使用范围扩展:

count = []
for i in xrange(6):
    count.extend(range(180,300,20))
或者简单地说:

 count range(180,300,20) * 6 `

您不需要while循环,您可以使用带有开始、停止和步骤的范围:

count = []
for i in xrange (6):
    for x in  xrange(180,300,20):
        count.append(x)
        print count
或使用范围扩展:

count = []
for i in xrange(6):
    count.extend(range(180,300,20))
或者简单地说:

 count range(180,300,20) * 6 `

count是一个整数,而不是列表。您需要使count=[],若要将内容添加到列表中,请使用。appendcount是一个整数,而不是列表。您需要使count=[],并且要将内容添加到列表中,您可以使用.appends。这样做,它会告诉我:count[i]=x indexer:list assignment index out ofrange@Versace是的,当您初始化列表时,清空它会引发索引错误,您需要使用append函数这样做,它告诉我:count[i]=x索引器错误:列表分配索引超出range@Versace是的,当您初始化列表时,如果列表为空,则会引发索引错误,您需要使用append函数