'range' object does not support item assig

2019-08-01 06:44发布

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条回答
神经病院院长
2楼-- · 2019-08-01 07:20

Use:

answer = list(range(4))

to allow modifications

查看更多
登录 后发表回答