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!"
Digital Brent
  • 1,275
  • 7
  • 19
  • 47
  • 1
    How are you selecting `p`? Is it an integer or a string? It it is a string, does it contain any whitespace? – Patrick Haugh Jan 03 '18 at 16:58
  • 1
    'p' could be a string, since 'c' is an int, '4' != 4 – APorter1031 Jan 03 '18 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)`. – heemayl Jan 03 '18 at 17:00
  • p comes from input (not raw_input) supplied by the user and then passed into a function containing this if statement – Digital Brent Jan 03 '18 at 17:00
  • Likely going to need to do `if str(c) == str(p):` or something along those lines. Depending on how you answer @PatrickHaugh of course. – Zack Tarr Jan 03 '18 at 17:01
  • I tried sticking the variables in the str() to make sure they were the same type, but I got the same results. – Digital Brent Jan 03 '18 at 17:06
  • That's odd. Python 2 `input()` _does_ return an `int` if the user types a string that can be converted to an integer. Please post a [mcve] that reproduces this problem. – PM 2Ring Jan 03 '18 at 17:07
  • BTW, you should generally avoid using Python 2 `input` because it calls `eval` on the user input, which makes it a security hole, but I guess it's not a big deal if you're the person supplying input to your own machine. After all, there are plenty of other ways to trash your own machine. ;) – PM 2Ring Jan 03 '18 at 17:10
  • 2
    As to your update: you cast place to int with `place_sym(int(place))`, but the grid contains strings. – Norrius Jan 03 '18 at 17:11

1 Answers1

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

dangee1705
  • 3,445
  • 1
  • 21
  • 40