How to draw a tic tac toe board in python?

0 votes
What are the relevant packages that we need to import to draw this?
Jun 26, 2019 in Python by Waseem
• 4,530 points
2,553 views

2 answers to this question.

0 votes

Following is a code to draw a tic tac toe board in python.

def draw_board():
    v = '|    |    |    |'
    h = ' ____ ____ ____ '
    for i in range(0,10):
        if i%3==0:
            print(h)
        else:
            print(v)
draw_board()

answered Jul 31, 2019 by Mohammad
• 3,230 points
Respected sir ,

your code is very simple and easy to understand but i have one problem how to put 'X' and 'o' in this board using this code . please suggest me

Hey, 

You can try this:

   def is_game_over():
        for a, b, c in WIN_COMBINATIONS:
            if board[a] == board[b] == board[c]:
                print("Player {0} wins!\n".format(board[a]))
                print("Congratulations!\n")
                return True
        if 9 == sum((pos == 'X' or pos == 'O') for pos in board):
            print("The game ends in a tie\n")
            return True

    for player in 'XO' * 9:
        draw()
        if is_game_over():
            break
        print("Player {0} pick your move".format(player))
        board[choose_number()] = player
        print()

while True:
    tic_tac_toe()
    if input("Play again (y/n)\n") != "y":
        break

I hope this will help.

0 votes

I also want to know the answer of this question. 

answered Jun 25 by anonymous

Hi, @There,

Did you try the above-given solution? I hope those will help you out for sure.

Related Questions In Python

0 votes
1 answer

How to add a new line in Python?

You can use '\n' for a next ...READ MORE

answered May 2, 2018 in Python by aayushi
• 750 points
310 views
0 votes
2 answers

How to calculate square root of a number in python?

calculate square root in python >>> import math ...READ MORE

answered Apr 2, 2019 in Python by anonymous
558 views
+1 vote
1 answer

How to print first character of each word in upper case of a string in Python

You can use split() using space as ...READ MORE

answered Jun 1, 2018 in Python by george
• 200 points
4,645 views
0 votes
1 answer

How to get the size of a string in Python?

If you are talking about the length ...READ MORE

answered Jun 4, 2018 in Python by aryya
• 7,370 points
215 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,089 views
0 votes
0 answers
+4 votes
6 answers
0 votes
1 answer

how to delete a file in python?

to delete a file import os os.remove('filename') this will delete ...READ MORE

answered Mar 18, 2019 in Python by Mohammad
• 3,230 points
41 views
0 votes
1 answer

How can I write a program to add two numbers using functions in python?

def add(a,b):      return a + b #when i call ...READ MORE

answered May 20, 2019 in Python by Mohammad
• 3,230 points
5,620 views