1

I'm taking a course online to learn python. I am working on a project to code a tic-tac-toe game. I've built this game around a nested list represented by the 3x3 tic-tac-toe grid. Here is the nested list:

grid = [[1,2,3],[4,5,6],[7,8,9]]

I'd like to prompt the user to select a number 1-9 and then replace the selected number in the nested list with either an X or an O depending on which player made the selection.

I can find the value inside the list just fine, and it will print out okay, along with the number that was entered by the user. However, when I try to compare the two values with an if statement, nothing happens. I'd like to just update the nested list value after the if statement but I can't figure out what I'm doing wrong or why the if statement won't fire. I can't get the value to update so I replaced that line with a print statement just to see how it would handle but the line I'm trying to print just gets ignored. Here is the if statement, where p = the number input by the user.

for r in grid:
    for c in r:
        print str(c) + " / " + str(p) # so I can see the values
        if c == p:
            print "Winner winner, chicken dinner!"

When I run the code and feed it an integer (in this case 4), I expect to see the chicken dinner line printed out but instead I just get the following:

1 / 4
2 / 4
3 / 4
4 / 4
5 / 4
6 / 4
7 / 4
8 / 4
9 / 4

Why doesn't it recognize that 4 == 4?

UPDATE: I tried sticking the variables in the str() to make sure they were the same type, but I got the same results. Here is the whole code so far:

grid = [['1','2','3'],['4','5','6'],['7','8','9']]
plyr = ("X","O")
turn = 0

def drw_brd():
    i = 1
    f = turn
    for spc in grid:
        print " " + spc[0] + " | " + spc[1] + " | " + spc[2] + " "
        if i<=2:
            print "-----------"
            i+=1

    print''
    print "Player %s (%s's) it's your turn!" %(str(f+1),plyr[turn])
    place = input('Cell number to take:')
    place_sym(int(place))
    check_win()

def check_win():
    switch_plyr()

def switch_plyr():
    global turn

    """
    if turn == 0:
        turn = 1
    else:
        turn = 0
    """
    if turn <= 0:
        turn = 1
    elif turn >= 1: 
        turn = 0

    #print turn
    drw_brd()

def place_sym(p):
    global turn
    global grid
    global plyr

    print plyr[turn]
    for r in grid:
        for c in r:
            print str(c) + " / " + str(p)
            if c == p:
                print "Winner winner, chicken dinner!"
9
  • 1
    How are you selecting p? Is it an integer or a string? It it is a string, does it contain any whitespace? Commented Jan 3, 2018 at 16:58
  • 1
    'p' could be a string, since 'c' is an int, '4' != 4 Commented Jan 3, 2018 at 17:00
  • 3
    Did you get p by input()? if so, p would be a string, not an integer. Cast it into an integer first: int(p). Commented Jan 3, 2018 at 17:00
  • p comes from input (not raw_input) supplied by the user and then passed into a function containing this if statement Commented Jan 3, 2018 at 17:00
  • 2
    As to your update: you cast place to int with place_sym(int(place)), but the grid contains strings. Commented Jan 3, 2018 at 17:11

1 Answer 1

1

The problem is that p is a string, c is an integer. Wherever you are getting your value for p (should look something like)

p = input("enter a number")

you should put

p = int(input("enter a number"))

this should fix your problem

Edit

Not all values were of the same type. Grid defined the numbers as strings,

grid = [['1','2','3'],['4','5','6'],['7','8','9']]

and input was running eval on the entered number, changing its type to an int, which meant the check for p == c returned False, as they were different types

Sign up to request clarification or add additional context in comments.

6 Comments

Well, I tried this, but maybe I'm doing it wrong. Didn't change anything :(
can you tell me what print(type(p)) and print(type(c)) output?
Yes hang on one sec
Ah, type of c is still str for some reason, I will try again to put that in an int
@DigitalBrent looking at your edit, you defined all the values in the grid as strings. you need to either have them as ints, and change the input, or have them as strings and leave the input alone
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.