Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python字符串分配错误发生在第二个循环上,但不是第一个循环_Python_Python 3.x_Typeerror - Fatal编程技术网

Python字符串分配错误发生在第二个循环上,但不是第一个循环

Python字符串分配错误发生在第二个循环上,但不是第一个循环,python,python-3.x,typeerror,Python,Python 3.x,Typeerror,while循环的第一次运行正常: hour_count = list('00/') hours = 0 while hours < 24: #loop while hours < 24 hour_count[1] = hours #<- error occurs on this line hour_count = ''.join(hour_count) #convert to string

while循环的第一次运行正常:

hour_count = list('00/')
hours = 0

while hours < 24:                    #loop while hours < 24

    hour_count[1] = hours            #<- error occurs on this line
    hour_count = ''.join(hour_count) #convert to string
    ...
    hours += 1
hour\u count=list('00/)
小时=0
小时数<24时:#小时数<24时循环

hour_count[1]=hours#当您运行此行时,
hour_count=''。join(hour_count)
,您正在将
hour_count
的数据类型从列表更改为字符串

因为字符串是不可变的,所以不能通过索引表示法修改一个字符(该行前面的行尝试这样做)

我不完全确定你的目标是什么,但也许你想把它添加到列表中。这些文档将对此有所帮助

您更改了类型

# hour_count at this point is an array
hour_count[1] = hours

# The result of join is a string object
hour_count = ''.join(hour_count)

下一次通过
hour\u count
的时间是一个字符串,您不能执行“string[1]=…”

hour\u count
以列表形式开始,但随后您将其更改为引用带有
连接的行上的字符串。