samedi 29 août 2015

How do I randomly select a variable from a list, and then modify it in python?

Here's my python 3 code. I would like to randomly select one of the cell variables (c1 through c9) and change its value to the be the same as the cpuletter variable.

import random

#Cell variables
c1 = "1"
c2 = "2"
c3 = "3"
c4 = "4"
c5 = "5"
c6 = "6"
c7 = "7"
c8 = "8"
c9 = "9"
cells = [c1, c2, c3, c4, c5, c6, c7, c8, c9]

cpuletter = "X"

random.choice(cells) = cpuletter

I'm getting a "Can't assign to function call" error on the "random.choice(cells)." I assume I'm just using it incorrectly? I know you can use the random choice for changing a variable like below:

import random
options = ["option1", "option2"]
choice = random.choice(options)




Aucun commentaire:

Enregistrer un commentaire