The first time my code loops through my nested while loop it works. But the second time it doesn't- I don't know why. Could someone tell me the reason.

``````m = [['__', '__', '__'],['__', '__', '__'],['__', '__', '__']]
z=1
count=9
f=[0,2]
from random import choice
w=choice(f)
from random import choice
t=choice(f)
i=[0,1,2]
print "Please insert an X or O into a coordinate point. (Top-left corner is (0,0) and bottom-right corner is (2,2)"
print m
d=raw_input("Would you like to go first? (Y/N) ")

if (d == 'N'):
for q in range(3):
for r in range(3):
if (m[q][r]=='__'):
if (m[w][t]=='__'):
m[w][t]='X'
print m
print "doda"
while  (z > 0):
a=input("Please insert a row coordinate ")
b=input("Please insert a column coordinate ")

while (m[a][b]!='__'):
print "Please select another cordinate pair"
a=input("Please insert a row coordinate ")
b=input("Please insert a column coordinate ")

if (m[a][b]=='__'):
m[a][b]='O'
print m

g=1

while  g== 1:
if (a == 0 or a == 2):
if (b == 0 or b == 2):
#range of values only corner can have-either 0 or 2
from random import choice
w=choice(f)
from random import choice
t=choice(f)
while (m[w][t]!='__'):
from random import choice
w=choice(f)
from random import choice
t=choice(f)
if (m[w][t]=='__'):
m[w][t]='X'
print m
count=count-1
print count
g=-1
print g``````

Maybe computer is like me and don't find its way through.

Do you know that you can use more than one letter for variable names?
Also you can write comments by # and comment. First line of function should have comment in triple quotes and explanation of what function is supposed to do. It is called docstring, see for example:
http://epydoc.sourceforge.net/docstrings.html

import statements should be in the beginning of the file and only once. Sorry, I can not read your code and tell what is supposed to do, I may only have one vaque guess starting with ti and with two dashes.

I have made a few edits, and have commented through the first line. But it is still not working.

``````from random import choice

m = [['__', '__', '__'],['__', '__', '__'],['__', '__', '__']]
z=1
count=9
f=[0,2]
w=choice(f)
t=choice(f)
i=[0,1,2]
print "Please insert an X or O into a coordinate point. (Top-left corner is (0,0) and bottom-right corner is (2,2)"
print m
d=raw_input("Would you like to go first? (Y/N) ")

if (d == 'N'):
for q in range(3):
for r in range(3):
if (m[q][r]=='__'):
if (m[w][t]=='__'):
m[w][t]='X'
print m
print "doda"
z=0
while  (z==0):
a=input("Please insert a row coordinate ")
b=input("Please insert a column coordinate ")

while (m[a][b]!='__'):
print "Please select another cordinate pair"
a=input("Please insert a row coordinate ")
b=input("Please insert a column coordinate ")

if (m[a][b]=='__'):
m[a][b]='O'
print m
z=1
g=1
print g
while  (g== 1):
if (a == 0 or a == 2):
if (b == 0 or b == 2):
#range of values only corner can have-either 0 or 2
w=choice(f)
t=choice(f)
while (m[w][t]!='__'):
w=choice(f)
t=choice(f)
if (m[w][t]=='__'):
m[w][t]='X'
print m
count=count-1
print count
g=-1
print g
z=0``````

I've run it with IDLE, but the major whileloop which begins at while (z==0):, doesn't run through a second time.

The first line wasn't a comment- but a tic tac toe board

but the major while loop which begins at while (z==0):, doesn't run through a second time.

Look for any lines of code that change "z", which is the only way to exit other than a break statement. Also, what happens if someone enters an "X" or any letter other than "N".

Nothing appears to be changing the value of z though. I have not written code for

what happens if someone enters an "X" or any letter other than "N".

yet,
because my current code is not working.

Could someone help?

OK. Here is one new start for you, if some of my idioms you do not understand, you must replace them with your style of code while code continues to run. If you make winner logic, user input part it should work.

This is the first version which is non-interactive. So I have little different strategy than griswolf, but principle is same. This would not be your first version for sure, but give you one fast start.

Read the code, it runs. It runs fast without interaction, so you can run it many times in loop to find if it crashes. In the end you replace the debug for with 'Another game' loop.

``````import random
computer_piece, user_piece = pieces = 'OX'

def random_place(board, turn):
x,y = random.randint(0,2), random.randint(0,2)
while board[x][y] != '_':
x,y = random.randint(0,2), random.randint(0,2)
return x,y

def choose_move(board, turn=0):
x, y = random_place(board, turn)
board[x][y] = pieces[turn]
return turn == 0

def winner(board):
""" This function will return False if board is not winner, winner's piece otherwise """
pass

def isfinnished(board):
return (winner(board) or
not any(x == '_' in row
for row in board
for x in row)     # check for full
)

def print_board(board):
print '-'*20
print '\n'.join(' '.join(line)
for line in board)

print "Wellcome to tic-tac-toe version 0.1 alpha"
print "Watch while I play against myself"
turn=0

for debug in range(20):
board = [
['_', '_', '_'],
['_', '_', '_'],
['_', '_', '_']]
# This loop does the stuff
while not isfinnished(board):
turn = choose_move(board, turn)
print_board(board)
raw_input('End of test run %i. Push enter.' % (debug+1))
print '='*60``````

I think the problem may be that you are not entering "N" for the "do you want to go first" question as you have an infinite while loop, and not one the doesn't repeat. The main problem with this code is that you have 54 lines of code that have not been tested. Take Tony's style, and some of his code, and start with a function to get the input. Check for a valid entry = an unoccupied row or column, and test it before going further (right now, you can enter 10 for the row and it will not catch it but will instead error out at "while (m[a]!='__'): )". Keep your functions short and sweet and you won't be confused. A maximum of 10 to 20 lines makes them easy to test and debug. Next on the coding list is a function to generate the computer's choice. After that is one function to fill the space chosen. You can pass "X" or "O", and the coordinates to the same function and use the same code for either, and test that function. Hopefully you get the idea.

This is a tic-tac-toe program that I coded some time ago. I doubt you will be able to use much of it, but it illustrates how much simpler it is when you break everything up into small pieces.

``````import random

class TicTacToe:
def __init__(self):
## 0=No winner,      1=Player #1 wins,
## 2=Player #2 wins, 3=Tie
self.winner = 0

self.board = [0, 0, 0, 0, 0, 0, 0, 0, 0,]
self.num_moves = [0, 0, 0]
self.player = 1
self.win_combos = (set([0, 1, 2]),
set([3, 4, 5]),
set([6, 7, 8]),
set([0, 3, 6]),
set([1, 4, 7]),
set([2, 5, 8]),
set([0, 4, 8]),
set([2, 4, 6]) )
self.play_a_game()

##-----------------------------------------------------------------
def play_a_game(self):
while not self.winner:
self.num_moves[self.player] += 1
##  a list of all squares not occupied
squares_to_choose = [ctr for ctr, square in enumerate(self.board) \
if square==0]
print "Choose from", squares_to_choose
##  choose a random available square
square = random.choice(squares_to_choose)
print("---> player %d occupies %d" % (self.player, square))
self.board[square] = self.player
self.print_squares()

winner = self.we_have_winner()
if not winner:
self.tie()

self.player = [0, 2, 1][self.player]

##-----------------------------------------------------------------
def print_squares(self):
for row in range(0, len(self.board), 3):
for col in range(3):
print self.board[row+col],
print

##-----------------------------------------------------------------
def tie(self):
if 0 not in self.board:
self.winner = 3

##-----------------------------------------------------------------
def we_have_winner(self):
""" extract all of the squares occupied for this player into
a set and check if it has a winning combo as a subset
"""
moves = set([ctr for ctr, person in enumerate(self.board) \
if person==self.player])
for win_combo in self.win_combos:
if win_combo.issubset(moves):
self.winner = self.player
print "winner=%s in %d moves" % \
(self.winner, self.num_moves[self.player]), win_combo

return True

return False

##-----------------------------------------------------------------
TT = TicTacToe()``````

what happens if someone enters an "X" or any letter other than "N".
yet,
because my current code is not working.

You don't want all of the code under the
if (d == 'N'):
loop. Instead try something like this:

``````# player=1 --> computer
# player=2 --> human
player = 2
if (d == 'N'):
player=1

winner = False
while not winner:
print "Getting choice for player", player
etc.``````