您好,我正在学习如何编程,我正在学习python。然而,我一直有一个程序,应该是改变字典值的问题。程序运行时会出现以下错误:
Traceback (most recent call last):
File "d:\programs\ailens.py", line 15, in <module>
ailen['color'] = 'green'
TypeError: 'str' object does not support item assignment
我的代码有什么问题:
ailens = {'color': 'green',
'spead':'slow'
,'points': 5 }
for ailen in ailens:
if ailen == 'slow':
ailen['color'] = 'yellow'
ailen['spead'] = 'medium'
ailen['points'] ='10'
elif ailen == 'medium':
ailen['color'] = 'red'
ailen['spead'] = 'fast'
ailen['ponits'] = '15'
else:
ailen['color'] = 'green'
ailen['spead'] = 'slow'
ailen['spead'] = 5
print(ailen)
当执行if语句时,for循环
ailen
指向的是键,而不是dict的值,它应该是ailens[ailen]
相关问题 更多 >
编程相关推荐