Total beginner wrote a tic tac toe game in Python and would like some feedback

2024/11/14 13:02:27

I've decided to learn Python about 2 weeks ago, been going through various books and videos, and I've decided to try my hand at programming a tic tac toe game. I was somewhat successful (it doesn't recognize if there's already a mark in a certain spot and allows overwriting of already placed marks) and I was wondering if any more experienced programmers could give me some general feedback about how I could do things better. Thank you so much and I hope you're all staying safe.

board = ['-'] * 9def print_board():print (board[0] + '|' + board[1] + '|' + board[2])print (board[3] + '|' + board[4] + '|' + board[5])print (board[6] + '|' + board[7] + '|' + board[8])legalMoves = [1,2,3,4,5,6,7,8,9]
print_board()turnCount = 0
def move():move = int(input('Pick a number 1-9:'))while move not in legalMoves:print('Illegal move')move = int(input('Pick a number 1-9:'))marks = ['X','O']if turnCount % 2 == 0:board[move - 1] = marks[1]else:board[move - 1] = marks[0]while True:if board[0] == board[1] == board[2] == 'X'\or board[3] == board[4] == board[5] == 'X'\or board[6] == board[7] == board[8] == 'X'\or board[0] == board[3] == board[6] == 'X'\or board[1] == board[4] == board[7] == 'X'\or board[2] == board[5] == board[8] == 'X'\or board[0] == board[4] == board[8] == 'X'\or board[2] == board[4] == board[6] == 'X' \or board[0] == board[1] == board[2] == 'O' \or board[3] == board[4] == board[5] == 'O' \or board[6] == board[7] == board[8] == 'O' \or board[0] == board[3] == board[6] == 'O' \or board[1] == board[4] == board[7] == 'O' \or board[2] == board[5] == board[8] == 'O' \or board[0] == board[4] == board[8] == 'O':print('Victory')breakelse:move()print_board()turnCount = turnCount + 1
Answer

well you need to check if the user has typed an int or something else, so that breaks your program, also prevent the user from overwriting existed cell input, also prevent the user from typing a number out of range which you have already taken care of that, so I have written this small function to give you the correct input and deals with wrong ones, you can use it

def isInt(strInt):for c in strInt:if c not in "0123456789": return Falsereturn Truedef getNextMove():while True:moveStr = input("Pick a number 1-9: ") if not isInt(moveStr): print("Pls write only a number")continuemove = int(moveStr)if move < 1 or move > 9: print("Pls only 1-9 numbers are allowed")continueif board[move - 1] != "-":print("Pls choose an empty cell")continuereturn move

Well, if that helped, I couldn't stop my self from writing the full code for the game, I couldn't sleep anyway, so here it is, you may get some ideas, happy coding!

board = [" "] * 9
winner = ""def printBoard():print("-" * 7)for i in range(3):print(f"|{board[i * 3]}|{board[i * 3 + 1]}|{board[i * 3 + 2]}|")print("-" * 7)def isInt(strInt):for c in strInt:if c not in "0123456789": return Falsereturn Truedef getNextMove():while True:moveStr = input("Pick a number 1-9: ") if not isInt(moveStr): print("Pls write only a number")continuemove = int(moveStr)if move < 1 or move > 9: print("Pls only 1-9 numbers are allowed")continueif board[move - 1] != " ":print("Pls choose an empty cell")continuereturn movedef checkHVD():for i in range(3):if board[i * 3] == board[i * 3 + 1] == board[i * 3 + 2] and board[i * 3] != " ":return board[i * 3]elif board[i] == board[i + 3] == board[i + 6] and board[i] != " ":return board[i]if (board[0] == board[4] == board[8] or board[2] == board[4] == board[6]) and board[4] != " ":return board[4]return Falsefor i in range(9):board[getNextMove() - 1] = i % 2 == 0 and "X" or "O"printBoard()winner = checkHVD();if winner:identity = winner == "X" and "first" or "second"print(f"The {identity} player({winner}) won")breakif not winner:print("It's a tie")
https://en.xdnf.cn/q/119463.html

Related Q&A

Separating tag attributes as a dictionary

My entry (The variable is of string type): <a href="https://wikipedia.org/" rel="nofollow ugc">wiki</a>My expected output: { href: https://wikipedia.org/, rel: nofollow …

How can I remove duplication of 2 separate which is interrelated with each other (PYTHON)

After reading so many title, I couldnt solved the problem below. Does anyone can help me please ? For instance, I have 2 list (list_I and list_II) which is interrelated with each other. list_I = [123,…

Array within an array?

Im trying to call up an element from an array within an array in Python. For example:array = [[a1,a2,a3,a4], [b1,b2,b3,b4], [c1,c2,c3,c4]]The question is, how would I print just the value b1?

How to create a zoned of gradation area on the edge of ROI in opencv python

I have a binary image (white and black), the where Region of Interest (ROI) is black. The shape of ROI is irregular and the location of ROI can be anywhere in the frame. I want to have a smooth gradati…

Prevent Terminal resize python curses

Im writing a program on python curses and I was wondering if there is a way to block terminal resizing in order to prevent curses crashing both on Linux and Windows. This is what happens.. Can I preven…

SymPy Not Doesnt Return LaTeX

Helloo! So, Im using SymPy to make a calculation for me. The trouble is, its output should be a LaTeX expression and in make case it prints something like SymPy Calculation Output Is there any way to s…

Python Flask: How to include JavaScript file for each template per blueprint

I have read Loading external script with jinja2 template directive and Import javascript files with jinja from static folder but unfortunately no closer I have a Python Flask site which is based on htt…

Difference between multiple elements in list with same string . Python 2.7 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking for code must demonstrate a minimal understanding of the problem being solved. Incl…

EDX Course API: Getting EDX course list

I am making a project in python/flask. I want to get a list of all the courses of edx. But the API provides the list page by page. I cant figure out how to get the entire list. Any help is appreciated.…

How to extract particlar message from a vast displayed output using python regular expression?

Firstly in the code, i would like to know How can i add a for loop for CH (1-11) instead of writing for every number Also how to extract SUCCESS and FAILED message from the output (reference) For examp…