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,540 points
11,486 views

3 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()

Hope this will help!

To learn more, go for Python Master course today.

Thank!

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, 2020 by anonymous

Hi, @There,

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

0 votes
def printTable():
            for i in range(1,10):
                    print(" ",end=' ')
                    if(i%3==0 and i!=9):
                            print("\n",'-'*8)
                    elif i!=9:
                            print('|',end=' ')
                            

Whole Game:
Used List for storing O and X.
Index as position from 1 to 9

1 | 2 | 3
 --------
4 | 5 | 6
 --------
7 | 8 | 9

P.S No package is required to be imported
Code available at
https://github.com/SumitNagpal94/MCA/tree/master/Python
answered Oct 11, 2020 by Sumit Nagpal

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
1,208 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
5,743 views
+1 vote
2 answers

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

class Solution:     def firstAlphabet(self, s):             self.s=s              k=''              k=k+s[0]              for i in range(len(s)):                     if ...READ MORE

answered Oct 28, 2020 in Python by Anurag
12,254 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,460 points
1,318 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

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

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
3,905 views
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
848 views
0 votes
2 answers

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

there is sum() function as a built ...READ MORE

answered Oct 25, 2020 in Python by anonymous
23,718 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP