1

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
  • 39,467
  • 16
  • 112
  • 140
Andrew
  • 11
  • 1
  • 3
  • Possible duplicate of [TypeError: 'range' object does not support item assignment](https://stackoverflow.com/questions/20484195/typeerror-range-object-does-not-support-item-assignment) – jdhao Nov 07 '17 at 01:55

1 Answers1

5

Use:

answer = list(range(4))

to allow modifications

jamylak
  • 128,818
  • 30
  • 231
  • 230
  • 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 May 08 '13 at 09: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 May 08 '13 at 09:19