# Help me with my code plz

i use vscode, and the .run extention is being weird, so it might be that, but idk. if you have any ideas on how to make this work, please let me know.

from random import randint

import time

global Save_R1_1

global Save_R2_1

global Save_R3_1

global Save_R1_2

global Save_R2_2

global Save_R3_2

def c_turn(Save_R1_1, Save_R2_1, Save_R3_1):

``````def c_move(R1, R2, R3):

randomizer1 = randint(1, 3)

randomizer2 = randint(1, 3)

if randomizer1 == 1:

if R1[randomizer2 - 1] == "_ ": #check later in turn sequence to see if spot is not open

R1[randomizer2 - 1] = "O"

Save_R1_1 = R1

Save_R2_1 = R2

Save_R3_1 = R3

elif randomizer1 == 2:

if R2[randomizer2 - 1] == "_ ": #check later in turn sequence to see if spot is not open

R2[randomizer2 - 1] = "O"

Save_R1_1 = R1

Save_R2_1 = R2

Save_R3_1 = R3

elif randomizer1 == 3:

if R2[randomizer2 - 1] == "_ ": #check later in turn sequence to see if spot is not open

R2[randomizer2 - 1] = "O"

Save_R1_1 = R1

Save_R2_1 = R2

Save_R3_1 = R3

return
``````

#Start computer move

#see, i first define vars

#i then exicute the actual move before the while loop, to establish secondary board saves for the while loop

#after that i initiate the while loop, eliminating any possibility that the computer will not move

#now i just need to do a wincheck… a very, very lengthly wincheck, including every possible win scinerio, that will take forever to write.

#well, that wasnt too bad… thank god for copy and paste. now i just print the board and move on to the players turn.

``````win = "tbd"

if Save_R1_1[0] and Save_R2_1[0] and Save_R3_1[0] and Save_R1_1[1] and Save_R2_1[1] and Save_R3_1[1] and Save_R1_1[2] and Save_R2_1[2] and Save_R3_1[2] != "_ ":

print("Tie. cya.")

exit()

Save_R1_2 = Save_R1_1

Save_R2_2 = Save_R2_1

Save_R3_2 = Save_R3_1

c_move(Save_R1_1, Save_R2_1, Save_R3_1)

while Save_R1_2 == Save_R1_1 and Save_R2_2 == Save_R2_1 and Save_R3_2 == Save_R3_1:

c_move(Save_R1_1, Save_R2_1, Save_R3_1)

if Save_R1_1[1] and Save_R1_1[2] and Save_R1_1[3] == "O ": win = "O"

elif Save_R2_1[1] and Save_R2_1[2] and Save_R2_1[3] == "O ": win = "O"

elif Save_R3_1[1] and Save_R3_1[2] and Save_R3_1[3] == "O ": win = "O"

elif Save_R1_1[1] and Save_R2_1[2] and Save_R3_1[3] == "O ": win = "O"

elif Save_R1_1[3] and Save_R2_1[2] and Save_R3_1[1] == "O ": win = "O"

elif Save_R1_1[1] and Save_R2_1[1] and Save_R3_1[1] == "O ": win = "O"

elif Save_R1_1[2] and Save_R2_1[2] and Save_R3_1[2] == "O ": win = "O"

elif Save_R1_1[3] and Save_R2_1[3] and Save_R3_1[3] == "O ": win = "O"

elif Save_R1_1[1] and Save_R1_1[2] and Save_R1_1[3] == "X ": win = "X"

elif Save_R2_1[1] and Save_R2_1[2] and Save_R2_1[3] == "X ": win = "X"

elif Save_R3_1[1] and Save_R3_1[2] and Save_R3_1[3] == "X ": win = "X"

elif Save_R1_1[1] and Save_R2_1[2] and Save_R3_1[3] == "X ": win = "X"

elif Save_R1_1[3] and Save_R2_1[2] and Save_R3_1[1] == "X ": win = "X"

elif Save_R1_1[1] and Save_R2_1[1] and Save_R3_1[1] == "X ": win = "X"

elif Save_R1_1[2] and Save_R2_1[2] and Save_R3_1[2] == "X ": win = "X"

elif Save_R1_1[3] and Save_R2_1[3] and Save_R3_1[3] == "X ": win = "X"

if win == "X":

print("Dang, look, you won...")

time.sleep(1)

print(Save_R1_1)

print(Save_R2_1)

print(Save_R3_1)

exit()

elif win == "O":

print("HAHA, look, i won!")

time.sleep(1)

print(Save_R1_1)

print(Save_R2_1)

print(Save_R3_1)

exit()

return Save_R1_1 and Save_R2_1 and Save_R3_1
``````

def p_turn(Save_R1_1, Save_R2_1, Save_R3_1):

``````win = "tbd"

time.sleep(1)

print(Save_R1_1)

print(Save_R2_1)

print(Save_R3_1)

if Save_R1_1[1] and Save_R2_1[1] and Save_R3_1[1] and Save_R1_1[2] and Save_R2_1[2] and Save_R3_1[2] and Save_R1_1[3] and Save_R2_1[3] and Save_R3_1[3] != "_ ":

print("Tie. cya.")

exit()

UI_X = input("What row would you like to move in? 1/2/3: ")

UI_Y = input("And what collum? 1/2/3: ")

if UI_X == "1":

Save_R1_1[int(UI_Y)]

if UI_X == "2":

Save_R2_1[int(UI_Y)]

if UI_X == "3":

Save_R3_1[int(UI_Y)]

if Save_R1_1[1] and Save_R1_1[2] and Save_R1_1[3] == "O ": win = "O"

elif Save_R2_1[1] and Save_R2_1[2] and Save_R2_1[3] == "O ": win = "O"

elif Save_R3_1[1] and Save_R3_1[2] and Save_R3_1[3] == "O ": win = "O"

elif Save_R1_1[1] and Save_R2_1[2] and Save_R3_1[3] == "O ": win = "O"

elif Save_R1_1[3] and Save_R2_1[2] and Save_R3_1[1] == "O ": win = "O"

elif Save_R1_1[1] and Save_R2_1[1] and Save_R3_1[1] == "O ": win = "O"

elif Save_R1_1[2] and Save_R2_1[2] and Save_R3_1[2] == "O ": win = "O"

elif Save_R1_1[3] and Save_R2_1[3] and Save_R3_1[3] == "O ": win = "O"

elif Save_R1_1[1] and Save_R1_1[2] and Save_R1_1[3] == "X ": win = "X"

elif Save_R2_1[1] and Save_R2_1[2] and Save_R2_1[3] == "X ": win = "X"

elif Save_R3_1[1] and Save_R3_1[2] and Save_R3_1[3] == "X ": win = "X"

elif Save_R1_1[1] and Save_R2_1[2] and Save_R3_1[3] == "X ": win = "X"

elif Save_R1_1[3] and Save_R2_1[2] and Save_R3_1[1] == "X ": win = "X"

elif Save_R1_1[1] and Save_R2_1[1] and Save_R3_1[1] == "X ": win = "X"

elif Save_R1_1[2] and Save_R2_1[2] and Save_R3_1[2] == "X ": win = "X"

elif Save_R1_1[3] and Save_R2_1[3] and Save_R3_1[3] == "X ": win = "X"

if win == "X":

print("Dang, look, you won...")

time.sleep(1)

print(Save_R1_1)

print(Save_R2_1)

print(Save_R3_1)

exit()

elif win == "O":

print("HAHA, look, i won!")

time.sleep(1)

print(Save_R1_1)

print(Save_R2_1)

print(Save_R3_1)

exit()

elif win == "tbd":

print(Save_R1_1)

print(Save_R2_1)

print(Save_R3_1)

return
``````

#begin syntax

print(“welcome to tic tac toe!”)

time.sleep(1)

print(“Im O, youre X! ill go first.”)

time.sleep(1)

Save_R1_1 = ["_ ", "_ ", "_ "]

Save_R2_1 = ["_ ", "_ ", "_ "]

Save_R3_1 = ["_ ", "_ ", "_ "]

c_turn(Save_R1_1, Save_R2_1, Save_R3_1)

p_turn(Save_R1_1, Save_R2_1, Save_R3_1)

By Evan Mottley via Discussions on Python.org at 31Mar2022 03:16:

i use vscode, and the .run extention is being weird, so it might be
that, but idk. if you have any ideas on how to make this work, please
let me know.

from random import randint
import time

global Save_R1_1
global Save_R2_1
global Save_R3_1
[…]

You do not need to do this. This just means these names are known at the
global (module) scope. Which they are already if you set them there,
which you probably do lower down. I’m saying this has no effect.

def c_turn(Save_R1_1, Save_R2_1, Save_R3_1):

These “Save_R1_1” variables etc are still local variables in the
`c_turn()` function. They are independent of the names in the global
scope. They get their values from whatever you pass to the `c_turn()`
function when you call it.

If you expect to change these, you need to return the new values when
you return from the function.

def c_move(R1, R2, R3):
[…]
Save_R1_1 = R1
Save_R2_1 = R2
[…]

This inner function assigns to variabnles named “Save_R1_1” etc. That
makes them local to this function. Distinct from the “Save_R1_1” in
c_turn()" and also distinct from the “Save_R1_1” in the global scope.

Then you continue `c_turn()`

if Save_R1_1[0] and Save_R2_1[0] and Save_R3_1[0] and Save_R1_1[1]
and Save_R2_1[1] and Save_R3_1[1] and Save_R1_1[2] and Save_R2_1[2]
and Save_R3_1[2] != "_ ":

I do not think this test does what you think it does.

I imagine you want to check that `Save_R1_1[0]!="_ "` and also that
`Save_R2_1[0]`!="_ "` and so on.

What the above test does is test the truthiness of `Save_R1_1[0]` and
the truthiness of `Save_R2_1[0]` and so on until the last condition,
which tests only that `Save_R3_1[2] != "_ "`.

Now, `Save_R1_1[0]` et al seem to be strings, and a nonempty string is
“true” (and empty string would be false). So all the leading conditions
are true, and only the final condition tests anything meaningful.

You probably want a much wordier condition, like this:

``````if (
Save_R1_1[0] != "_ "
and Save_R2_1[0] != "_ "
and Save_R3_1[0] != "_ "
... and so on ...
):
``````
``````   print("Tie. cya.")
exit()
``````

This `exit()` exits the entire programme, not just this function. That
might work ok for a small programme, but it is almost always not the way
to go in a large programme.

Save_R1_1 = ["_ ", "_ ", "_ “]
Save_R2_1 = [”_ ", "_ ", "_ “]
Save_R3_1 = [”_ ", "_ ", "_ "]
c_turn(Save_R1_1, Save_R2_1, Save_R3_1)
p_turn(Save_R1_1, Save_R2_1, Save_R3_1)

Because the various “Save_R1_1” variables are local to each function,
none of the functions has any effect on the outer names.

For `c_turn()` you probably want to return the new values, so end it
with:

``````return Save_R1_1, Save_R2_1, Save_R3_1
``````

and call it like this in the code above:

``````Save_R1_1, Save_R2_1, Save_R3_1 = c_turn(Save_R1_1, Save_R2_1, Save_R3_1)
``````

The `c_move` can be modified the same way: pass in `Save_R1_1, Save_R2_1, Save_R3_1`, and get them back out.

There are other approaches involving `nonlocal` and closures, but I
really want to to approach this with parameters and return values
instead. This reliance on “global” names which are known and affected
everywhere directly is going to cause you trouble with any larger
programme.

I would even go so far as to suggest getting rid of all the global
variables. Drop the `global` statements at the top. Change the bottom to
look like this:

``````def main():
Save_R1_1 = ["_ ", "_ ", "_ "]
Save_R2_1 = ["_ ", "_ ", "_ "]
Save_R3_1 = ["_ ", "_ ", "_ "]
Save_R1_1, Save_R2_1, Save_R3_1 = c_turn(Save_R1_1, Save_R2_1, Save_R3_1)
p_turn(Save_R1_1, Save_R2_1, Save_R3_1)

main()
``````

With this change, all variables are local and the only way to effect
change with functions is to pass values in and get changed values ack
out from the `return` statements, which is what is happening on this
line:

``````Save_R1_1, Save_R2_1, Save_R3_1 = c_turn(Save_R1_1, Save_R2_1, Save_R3_1)
``````

Cheers,
Cameron Simpson cs@cskk.id.au