'range' object does not support item assignment - trying to use old python code in python 3.3

耗尽温柔 提交于 2019-12-24 10:56:20

问题


I am trying to run an old python code made with python 2.7(?) in python 3.3 and I'm stuck on updating the code to run. It keeps telling me "'range' object does not support item assignment" and for the life of I cannot figure it out. The code is for a "50 states trivia" game I found on google.

The error is at the line answer[i] = "%s " % flower[pick[i]].rstrip()

 pick = random.sample(range(50), 4)

print("The state flower of %s is:" % state[pick[0]])
answer = range(4)
for i in range(4):
    if i == 0:
        answer[i] = "%s " % flower[pick[i]].rstrip()
    else:
        answer[i] = "%s" % flower[pick[i]].rstrip()

BTW, this code is HERE


回答1:


Use:

answer = list(range(4))

to allow modifications



来源:https://stackoverflow.com/questions/16435607/range-object-does-not-support-item-assignment-trying-to-use-old-python-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!