“范围"对象不支持项目分配 - 尝试在 python 3.3 中使用旧的 python 代码 [英] 'range' object does not support item assignment - trying to use old python code in python 3.3

查看:51
本文介绍了“范围"对象不支持项目分配 - 尝试在 python 3.3 中使用旧的 python 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 python 3.3 中运行用 python 2.7(?) 制作的旧 python 代码,但我一直在更新要运行的代码.它一直告诉我'范围'对象不支持项目分配"并且我一生都无法弄清楚.该代码适用于我在谷歌上找到的50 个州琐事"游戏.

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.

错误在 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

推荐答案

使用:

answer = list(range(4))

允许修改

这篇关于“范围"对象不支持项目分配 - 尝试在 python 3.3 中使用旧的 python 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆