vendredi 27 janvier 2017

Python: Randomly choosing a variable and assigning a value to that variable

I am a Python and coding noob. Only started a few weeks ago. I am writing a program that will essentially be a tic-tac-toe game. It uses a simple text based drawing of the board game using - lines and + pluses. In the beginning of the program, I have the 9 spaces in the tic-tac-toe board defined as the following:

valuea1 = " "
valuea2 = " "
valuea3 = " "
valueb1 = " "

And so on through c3... The blank spaces are set up to allow substitution of an X or an O. My board looks like this:

def board():
    """ prints the tic-tac-toe board"""
    print(" +---A------B------C--+")
    print(" |      |      |      |")
    print("1|  " + valuea1 +"   |  " + valueb1 +"   |  " + valuec1 + "   |")
    print(" |      |      |      |")
    print(" ----------------------")
    print(" |      |      |      |")
    print("2|  " + valuea2 +"   |  " + valueb2 +"   |  " + valuec2 + "   |")
    print(" |      |      |      |")
    print(" ----------------------")
    print(" |      |      |      |")
    print("3|  " + valuea3 +"   |  " + valueb3 +"   |  " + valuec3 + "   |")
    print(" |      |      |      |")
    print(" +--------------------+")
    return

Later on in the program, based on an if statement, the symbol the "computer player" will be using (either X or O) is stored in the variable:

computer_mark ## is either "X" or "O" based on player choice

Now here is my main concern and question

randomlist = ("valuea1", "valuec1", "valuea3", "valuec3")
random_result = random.choice(randomlist)
## (REPLACE WITH CODE)
## takes the value stored in random_result, somehow treats the value as a variable,
## assigns that given variable the string value that is stored in computer_mark

I try to randomly choose 1 of 4 spots on the board, and change that spot from a " " string value to the string value stored in computer_mark.

Is there a way to code Python into choosing a random variable, and assigning it a value that is stored in a different variable?




Aucun commentaire:

Enregistrer un commentaire