Python字符串分配错误发生在第二个循环上,但不是第一个循环
while循环的第一次运行正常: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
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
以列表形式开始,但随后您将其更改为引用带有连接的行上的字符串。