Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

why is it not putting x or o instead of the space

3 Answers

0 votes
answered Mar 8 by Aryan singh (140 points)
0 votes
answered Mar 9 by Peter Minarik (55,660 points)

More work needs to be done

There are quite a number of mistakes in your code. Let me point out a few and you can work on the rest to make your game work.

Use The Right Types

x_move=input ("it is x's turn where would you like to go (1-9): ")
if (x_move==1):
    board[1]=x

The 1st line above takes an input and stores it in x_move. input() returns a type of string, so x_move is a string.

The 2nd line checks if x_move is 1. That is, it compares the variable x_move that is a string to the number 1. This comparison will not succeed. You should compare numbers to numbers. You can easily achieve this by casting the result of input() to an integral number (int).

The 3rd line tries to set an element in a list to x. However, x is not a known variable so the code will stop executing that line. What you should do here is to set the element of the list to the character x, which is denoted by 'x'.

To fix all the above 3 lines you should write something like this:

x_move=(int)(input("it is x's turn where would you like to go (1-9): "))
if (x_move==1):
    board[1]='x'

Use The Right Variables

Consider this piece of code of yours:

o_move=input ("it is o's turn where would you like to go (1-9): ")
if (x_move==1):
    board[1]=o

Apart from what I've explained in the previous section ("Use The Right Types") notice that when you think the user entered 1, you try to put the o marker in its place. However, you do not compare the value the user just entered (o_move), but the previous value (x_move) incorrectly.

Confusing Condition In While

Let's look at the below piece of code of yours (broken into multiple lines to make it more readable:

while (not board[1] or
       " "==board[3] or " "==board[5] or
       " " and
       not board[13]==board[15]==board[17] and
       not board[24]==board[26]==board[28] and
       not board[1]==board[15]==board[28] and
       not board[5]==board[15]==board[24]):

I think what you're trying to do in the last 4 lines is to check horizontal and diagonal matches. However, there are 3 horizontal lines to check and you only check 2. What about the vertical lines?

I am not sure what you tried to achieve with the first line. board[1] is a character. A character cast into a boolean (as only logical expressions can for a condition) is always true. Hence the first line is always false (not board[1]) and since its value cannot change it doesn't serve any purpose.

The second line is checking if positions (from the user's point of view) 2 and 3 have not been set. Why is this information relevant?

The third line is a string. A string is always true as long as it's not empty. Another expression that does not change its value so it's pointless to add it to the condition.

What you should do here is this:

  1. Check if there are any steps left to take (either count the steps or check if the board has empty cells)
  2. Check vertically (x3), horizontally (x3) and diagonally (x2) if there are any matches

And that's it. If either of the above is true, it's game over. If both of them are false, the game keeps going.

Mixing Data And View

It is generally a good idea to separate your data from your view. Right now, your data (board) is the view as well. It would make sense to have a board for a 3x3 matrix dedicated for ' ', 'o' and 'x' only, and a separate function for printing the board to the user with all the vertical and horizontal separators.

Good luck!

commented Mar 9 by Sam (1,310 points)
wait so how do I check if they're equal and not a space
0 votes
answered Apr 12 by userdotexe (1,330 points)

I forked and adjusted your code, you can find the revised version here: https://onlinegdb.com/lPaOud6qy.

By the way, your tic-tac-toe game is very impressive for a just 49 line program.

:)

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...