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
codeforester
43.7k21 gold badges121 silver badges157 bronze badges
-
Possible duplicate of TypeError: 'range' object does not support item assignmentjdhao– jdhao2017年11月07日 01:55:55 +00:00Commented Nov 7, 2017 at 1:55
1 Answer 1
Use:
answer = list(range(4))
to allow modifications
answered May 8, 2013 at 8:07
-
Thank you! But now I'm having all kinds of problems. Maybe someone could take a look at the original code (I added a link)? I'm getting a "local variable 'correct' referenced before assignment", and none of the multiple choice answers are displaying, just numbers.Andrew– Andrew2013年05月08日 09:18:31 +00:00Commented May 8, 2013 at 9:18
-
@Andrew No worries, but sorry this site doesn't look at links, we need questions to be self contained. If you would like you can submit a new question :)jamylak– jamylak2013年05月08日 09:19:58 +00:00Commented May 8, 2013 at 9:19
Explore related questions
See similar questions with these tags.
lang-py