为什么python我的字典会出现类型错误(即srt)?python 3

2024-06-17 16:09:32 发布

您现在位置:Python中文网/ 问答频道 /正文

您好,我正在学习如何编程,我正在学习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)

Tags: in程序程序运行字典编程错误greenpoints