Hi, I am trying to write a connect four program in python, which I am not very experienced at. This is what I have so far:

row7 = [' ', ' ', ' ', ' ', ' ', ' ', ' ']
row6 = [' ', ' ', ' ', ' ', ' ', ' ', ' ']
row5 = [' ', ' ', ' ', ' ', ' ', ' ', ' ']
row4 = [' ', ' ', ' ', ' ', ' ', ' ', ' ']
row3 = [' ', ' ', ' ', ' ', ' ', ' ', ' ']
row2 = [' ', ' ', ' ', ' ', ' ', ' ', ' ']
row1 = [' ', ' ', ' ', ' ', ' ', ' ', ' ']

row1 = list(row1)
row2 = list(row2)
row3 = list(row3)
row4 = list(row4)
row5 = list(row5)
row6 = list(row6)
def make_board():
    print "6| %s | %s | %s | %s | %s | %s | %s |" % tuple((row6))
    print "5| %s | %s | %s | %s | %s | %s | %s |" % tuple((row5))
    print "4| %s | %s | %s | %s | %s | %s | %s |" % tuple((row4))
    print "3| %s | %s | %s | %s | %s | %s | %s |" % tuple((row3))
    print "2| %s | %s | %s | %s | %s | %s | %s |" % tuple((row2))
    print "1| %s | %s | %s | %s | %s | %s | %s |" % tuple((row1))
    print "-| a | b | c | d | e | f | g |"
make_board()
def p1move():
    while True:
        if p1move1=='a':
            row1[0]='X'
            make_board()
            break
        elif p1move1=='b':
            row1[1]=='X'
            make_board()
            break
        elif p1move1=='c':
            row1[2]=='X'
            make_board()
            break
        elif p1move1=='d':
            row1[3]=='X'
            make_board()
            break
        elif p1move1=='e':
            row1[4]=='X'
            make_board()
            break
        elif p1move1=='f':
            row1[5]=='X'
            make_board()
            break
        elif p1move1=='g':
            row1[6]=='X'
            make_board()
            break
        elif p1move1 != 'a' or 'b' or 'c' or 'd' or 'e' or 'f' or 'g':
            print 'Please enter a letter a-g'
        else:
            print "Invalid input, try again."
            
p1move1=raw_input('Welcome to Connect Four, you are X, enter a lettered row to make a move!: ')
p1move()
def p2move():
        while True:
            if p2move1=='a'and p1move1=='a':
                row2[0]='O'
            elif p2move1=='a':
                row1[0]='O'
                make_board()
            break
            if p2move1=='b'and p1move1=='b':
                row2[1]=='O'
            elif p2move1=='b':
                row1[1]='O'
                make_board()
            break
            if p2move1=='c'and p1move1=='c':
                row2[2]=='O'
            elif p2move1=='c':
                row1[2]='O'
                make_board()
            break
            if p2move1=='d'and p1move1=='d':
                row2[3]=='O'
            elif p2move1=='d':
                row1[3]='O'
                make_board()
            break
            if p2move1=='e'and p1move1=='e':
                row2[4]=='O'
            elif p2move1=='e':
                row1[4]='O'
                make_board()
            break
            if p2move1=='f'and p1move1=='f':
                row2[5]=='O'
            elif p2move1=='f':
                row1[5]='O'
                make_board()
            break
            if p2move1=='g'and p1move1=='g':
                row2[6]=='O'
            elif p2move1=='g':
                row1[6]='O'
                make_board()
            else:
                print "Invalid input, try again."
                
p2move1=raw_input('Welcome to Connect Four, you are O, enter a lettered row to make a move!: ')
p2move()
make_board()

If I dont enter 'a' in the first input the variable 'X' is not assigned to the board (row1). The same problem exists with the player 2 move. Also do I have to keep repeating a function like p1move and p2move over and over? Is there an easier way to do this? Thanks and any help is appreciated.

Recommended Answers

All 16 Replies

add

global p1move1
global p2move1

to the start of your program, that may help.

ok I made them globals, I had that before but didn't know if it did anything. Now still when I enter 'b' for the first players turn it doesn't change the list. How do I do this?
thanks

1) Compare the break indentation in p1move() to p2move()
also keep your indentation levels uniform

2) elif p1move1 != 'a' or 'b' or 'c' or 'd' or 'e' or 'f' or 'g':
should be elif p1move1 not in 'abcdefg':

3) There is a lot of unneeded code

4) All those repetitive statements cry out for loops

Instead of 7 variables row1, row2, etc, you could have one variable rows, which is an array of rows

rows = [ [' ' for i in range(7)] for j in range(7)]

template = " %s |" * 7
def make_board():
    for i in range(6, -1, -1):
        print(("%d |" %(i+1)) + (template % tuple(rows[i])))
    print("-" * 31)
    print("  |" + (template % tuple("abcdefg")))

Now your rows are rows[0], rows[1], ..., rows[6] . This makes it easier to write loops.

Here is how you could write a main loop for your game. Add this to the above post

def main():
    print("Welcome to Connect Four")
    players = "OX"
    playerIndex = 0
    while True:
        # The game's mainloop, we iterate over the players
        make_board()
        playerIndex = 1 - playerIndex # alternatively 1 or 0
        player = players[playerIndex] # This is 'O' or 'X'
        move = raw_input("You are %s. Enter a lettered row to make a move! " % player)
        while True:
            # Inner loop: we iterate until valid input
            move = move.strip() # remove trailing white space
            try:
                if move == 'q':
                    # player entered 'q' to exit
                    raise SystemExit
                column = "abcdefg".index(move)
                row = find_row(column)
                break # we exit inner loop
            except ValueError:
                move = raw_input("Invalid input, try again: ")
        pmove(player, row, column)

def find_row(column):
    for i in range(7):
        if rows[i][column] == ' ':
            return i
    raise ValueError

def pmove(player, row, column):
    rows[row][column] = player

main()

There is still a lot to do. You must check if a player wins and take appropriate action.

ok, i got everything to work...thanks for the help you guys. Now I need the winning check combos. I spent time figuring out all this but where do I put the function in to check it?

def WinningCombos():
    if row1[0] and row1[1] and row1[2] and row1[3]=='X':
        return "X Wins"
    elif row1[1] and row1[2] and row1[3] and row1[4]=='X':
        return "X Wins"
    elif row1[2] and row1[3] and row1[4] and row1[5]=='X':
        return "X Wins"
    elif row2[0] and row2[1] and row2[2] and row2[3]=='X':
        return "X Wins"
    elif row2[1] and row2[2] and row2[3] and row2[4]=='X':
        return "X Wins"
    elif row2[2] and row2[3] and row2[4] and row2[5]=='X':
        return "X Wins"
    elif row3[0] and row3[1] and row3[2] and row3[3]=='X':
        return "X Wins"
    elif row3[1] and row3[2] and row3[3] and row3[4]=='X':
        return "X Wins"
    elif row3[2] and row3[3] and row3[4] and row3[5]=='X':
        return "X Wins"
    elif row4[0] and row4[1] and row4[2] and row4[3]=='X':
        return "X Wins"
    elif row4[1] and row4[2] and row4[3] and row4[4]=='X':
        return "X Wins"
    elif row4[2] and row4[3] and row4[4] and row4[5]=='X':
        return "X Wins"
    elif row5[0] and row5[1] and row5[2] and row5[3]=='X':
        return "X Wins"
    elif row5[1] and row5[2] and row5[3] and row5[4]=='X':
        return "X Wins"
    elif row5[2] and row5[3] and row5[4] and row5[5]=='X':
        return "X Wins"
    elif row6[0] and row6[1] and row6[2] and row6[3]=='X':
        return "X Wins"
    elif row6[1] and row6[2] and row6[3] and row6[4]=='X':
        return "X Wins"
    elif row6[2] and row6[3] and row6[4] and row6[5]=='X':
        return "X Wins"
    elif row7[0] and row7[1] and row7[2] and row7[3]=='X':
        return "X Wins"
    elif row7[1] and row7[2] and row7[3] and row7[4]=='X':
        return "X Wins"
    elif row7[2] and row7[3] and row7[4] and row7[5]=='X':
        return "X Wins"
    elif row1[1] and row2[1] and row3[1] and row4[1]=='X':
        return "X Wins"
    elif row2[1] and row3[1] and row4[1] and row5[1]=='X':
        return "X Wins"
    elif row3[1] and row4[1] and row5[1] and row6[1]=='X':
        return "X Wins"
    elif row1[2] and row2[2] and row3[2] and row4[2]=='X':
        return "X Wins"
    elif row2[2] and row3[2] and row4[2] and row5[2]=='X':
        return "X Wins"
    elif row3[2] and row4[2] and row5[2] and row6[2]=='X':
        return "X Wins"
    elif row1[3] and row2[3] and row3[3] and row4[3]=='X':
        return "X Wins"
    elif row2[3] and row3[3] and row4[3] and row5[3]=='X':
        return "X Wins"
    elif row3[3] and row4[3] and row5[3] and row6[3]=='X':
        return "X Wins"
    elif row1[4] and row2[4] and row3[4] and row4[4]=='X':
        return "X Wins"
    elif row2[4] and row3[4] and row4[4] and row5[4]=='X':
        return "X Wins"
    elif row3[4] and row4[4] and row5[4] and row6[4]=='X':
        return "X Wins"
    elif row1[5] and row2[5] and row3[5] and row4[5]=='X':
        return "X Wins"
    elif row2[5] and row3[5] and row4[5] and row5[5]=='X':
        return "X Wins"
    elif row3[5] and row4[5] and row5[5] and row6[5]=='X':
        return "X Wins"
    elif row1[0] and row2[1] and row3[2] and row4[3]=='X':
        return "X Wins"
    elif row2[0] and row3[1] and row4[2] and row5[3]=='X':
        return "X Wins"
    elif row3[0] and row4[1] and row5[2] and row6[3]=='X':
        return "X Wins"
    elif row4[0] and row5[1] and row6[2] and row7[3]=='X':
        return "X Wins"
    elif row1[1] and row2[2] and row3[3] and row4[4]=='X':
        return "X Wins"
    elif row2[1] and row3[2] and row4[3] and row5[4]=='X':
        return "X Wins"
    elif row3[1] and row4[2] and row5[3] and row6[4]=='X':
        return "X Wins"
    elif row4[1] and row5[2] and row6[3] and row7[4]=='X':
        return "X Wins"
    elif row1[2] and row2[3] and row3[4] and row4[5]=='X':
        return "X Wins"
    elif row2[2] and row3[3] and row4[4] and row5[5]=='X':
        return "X Wins"
    elif row3[2] and row4[3] and row5[4] and row6[5]=='X':
        return "X Wins"
    elif row4[2] and row5[3] and row6[4] and row7[5]=='X':
        return "X Wins"
    elif row7[0] and row6[1] and row5[2] and row4[3]=='X':
        return "X Wins"
    elif row6[0] and row5[1] and row4[2] and row3[3]=='X':
        return "X Wins"
    elif row5[0] and row4[1] and row3[2] and row2[3]=='X':
        return "X Wins"
    elif row4[0] and row3[1] and row2[2] and row1[3]=='X':
        return "X Wins"
    elif row7[1] and row6[2] and row5[3] and row4[4]=='X':
        return "X Wins"     
    elif row6[1] and row5[2] and row4[3] and row3[4]=='X':
        return "X Wins"     
    elif row5[1] and row4[2] and row3[3] and row2[4]=='X':
        return "X Wins"     
    elif row4[1] and row3[2] and row2[3] and row1[4]=='X':
        return "X Wins"     
    elif row7[2] and row6[3] and row5[4] and row4[5]=='X':
        return "X Wins"     
    elif row6[2] and row5[3] and row4[4] and row3[5]=='X':
        return "X Wins"     
    elif row5[2] and row4[3] and row3[4] and row2[5]=='X':
        return "X Wins"     
    elif row4[2] and row3[3] and row2[4] and row1[5]=='X':
        return "X Wins"
    elif row1[0] and row1[1] and row1[2] and row1[3]=='O':
        return "O Wins"
    elif row1[1] and row1[2] and row1[3] and row1[4]=='O':
        return "O Wins"
    elif row1[2] and row1[3] and row1[4] and row1[5]=='O':
        return "O Wins"
    elif row2[0] and row2[1] and row2[2] and row2[3]=='O':
        return "O Wins"
    elif row2[1] and row2[2] and row2[3] and row2[4]=='O':
        return "O Wins"
    elif row2[2] and row2[3] and row2[4] and row2[5]=='O':
        return "O Wins"
    elif row3[0] and row3[1] and row3[2] and row3[3]=='O':
        return "O Wins"
    elif row3[1] and row3[2] and row3[3] and row3[4]=='O':
        return "O Wins"
    elif row3[2] and row3[3] and row3[4] and row3[5]=='O':
        return "O Wins"
    elif row4[0] and row4[1] and row4[2] and row4[3]=='O':
        return "O Wins"
    elif row4[1] and row4[2] and row4[3] and row4[4]=='O':
        return "O Wins"
    elif row4[2] and row4[3] and row4[4] and row4[5]=='O':
        return "O Wins"
    elif row5[0] and row5[1] and row5[2] and row5[3]=='O':
        return "O Wins"
    elif row5[1] and row5[2] and row5[3] and row5[4]=='O':
        return "O Wins"
    elif row5[2] and row5[3] and row5[4] and row5[5]=='O':
        return "O Wins"
    elif row6[0] and row6[1] and row6[2] and row6[3]=='O':
        return "O Wins"
    elif row6[1] and row6[2] and row6[3] and row6[4]=='O':
        return "O Wins"
    elif row6[2] and row6[3] and row6[4] and row6[5]=='O':
        return "O Wins"
    elif row7[0] and row7[1] and row7[2] and row7[3]=='O':
        return "O Wins"
    elif row7[1] and row7[2] and row7[3] and row7[4]=='O':
        return "O Wins"
    elif row7[2] and row7[3] and row7[4] and row7[5]=='O':
        return "O Wins"
    elif row1[1] and row2[1] and row3[1] and row4[1]=='O':
        return "O Wins"
    elif row2[1] and row3[1] and row4[1] and row5[1]=='O':
        return "O Wins"
    elif row3[1] and row4[1] and row5[1] and row6[1]=='O':
        return "O Wins"
    elif row1[2] and row2[2] and row3[2] and row4[2]=='O':
        return "O Wins"
    elif row2[2] and row3[2] and row4[2] and row5[2]=='O':
        return "O Wins"
    elif row3[2] and row4[2] and row5[2] and row6[2]=='O':
        return "O Wins"
    elif row1[3] and row2[3] and row3[3] and row4[3]=='O':
        return "O Wins"
    elif row2[3] and row3[3] and row4[3] and row5[3]=='O':
        return "O Wins"
    elif row3[3] and row4[3] and row5[3] and row6[3]=='O':
        return "O Wins"
    elif row1[4] and row2[4] and row3[4] and row4[4]=='O':
        return "O Wins"
    elif row2[4] and row3[4] and row4[4] and row5[4]=='O':
        return "O Wins"
    elif row3[4] and row4[4] and row5[4] and row6[4]=='O':
        return "O Wins"
    elif row1[5] and row2[5] and row3[5] and row4[5]=='O':
        return "O Wins"
    elif row2[5] and row3[5] and row4[5] and row5[5]=='O':
        return "O Wins"
    elif row3[5] and row4[5] and row5[5] and row6[5]=='O':
        return "O Wins"
    elif row1[0] and row2[1] and row3[2] and row4[3]=='O':
        return "O Wins"
    elif row2[0] and row3[1] and row4[2] and row5[3]=='O':
        return "O Wins"
    elif row3[0] and row4[1] and row5[2] and row6[3]=='O':
        return "O Wins"
    elif row4[0] and row5[1] and row6[2] and row7[3]=='O':
        return "O Wins"
    elif row1[1] and row2[2] and row3[3] and row4[4]=='O':
        return "O Wins"
    elif row2[1] and row3[2] and row4[3] and row5[4]=='O':
        return "O Wins"
    elif row3[1] and row4[2] and row5[3] and row6[4]=='O':
        return "O Wins"
    elif row4[1] and row5[2] and row6[3] and row7[4]=='O':
        return "O Wins"
    elif row1[2] and row2[3] and row3[4] and row4[5]=='O':
        return "O Wins"
    elif row2[2] and row3[3] and row4[4] and row5[5]=='O':
        return "O Wins"
    elif row3[2] and row4[3] and row5[4] and row6[5]=='O':
        return "O Wins"
    elif row4[2] and row5[3] and row6[4] and row7[5]=='O':
        return "O Wins"
    elif row7[0] and row6[1] and row5[2] and row4[3]=='O':
        return "O Wins"
    elif row6[0] and row5[1] and row4[2] and row3[3]=='O':
        return "O Wins"
    elif row5[0] and row4[1] and row3[2] and row2[3]=='O':
        return "O Wins"
    elif row4[0] and row3[1] and row2[2] and row1[3]=='O':
        return "O Wins"
    elif row7[1] and row6[2] and row5[3] and row4[4]=='O':
        return "O Wins"     
    elif row6[1] and row5[2] and row4[3] and row3[4]=='O':
        return "O Wins"     
    elif row5[1] and row4[2] and row3[3] and row2[4]=='O':
        return "O Wins"     
    elif row4[1] and row3[2] and row2[3] and row1[4]=='O':
        return "O Wins"     
    elif row7[2] and row6[3] and row5[4] and row4[5]=='O':
        return "O Wins"     
    elif row6[2] and row5[3] and row4[4] and row3[5]=='O':
        return "O Wins"     
    elif row5[2] and row4[3] and row3[4] and row2[5]=='O':
        return "O Wins"     
    elif row4[2] and row3[3] and row2[4] and row1[5]=='O':
        return "O Wins"

Gribouillis, I greatly appreciate your help, but I do not understand some of the items that you used in your code. Such as: try, except, raise, SystemExit, and valueerror. Where would you suggest I learn about these? Or is there a brief explanation for them. Thanks a ton.

You can learn about exceptions here http://www.diveintopython.org/file_handling/index.html and here http://docs.python.org/tutorial/errors.html#exceptions. In the above code, I'm using the line

column = "abcdefg".index(move)

This expression returns the position of move in the string "abcdefg". For example if move is 'a', it returns 0, if move is 'b', it returns 1, etc. Now suppose that move is 'z'. The call to index will fail, and it does this by raising an 'exception'. Here the type of this exception is ValueError. If we do nothing, the program exits and prints a 'traceback' like this

>>> column = "abcdefg".index('z')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found

What we can do is 'catch' the exception with a try...except statement like this

try:
    column = "abcdefg".index(move)
except ValueError:
   print("We didn't compute column, but we caught the exception")

The statements in the except part are executed if one of the statements in the try part failed with a ValueError exception. So the program won't crash on this error and we can decide what we want to do when 'move' is not in the string "abcdefg".

ok great, now all i need to do is figure out where to put the winning function.
It is easy to learn how to put pictures in, using pygame or something else. To make a board and pieces?

I'm afraid I can't help you with pygame, but perhaps other members can help you :)

This is the final code, except the program is not identifying the win_combos as a function. I have tried many things, even rewriting it a few ways, but the program doesn't accept it. I know that it is correct, but what is wrong? Also where do I put it to make it check each time the program ask for the input of the row? Thanks

#ConnectFour
#Gabe Cohen

rows = [ [' ' for i in range(7)] for j in range(7)]

connect4board = " %s |" * 7
def make_board():
    for i in range(6, -1, -1):
        print(("%d |" %(i+1)) + (connect4board % tuple(rows[i])))
    print("-" * 31)
    print("  |" + (connect4board % tuple("abcdefg")))

def main():
    print("Welcome to Connect Four!")
    players = "OX"
    playerIndex = 0
    while True:
        # The game's mainloop, we iterate over the players
        make_board()
        playerIndex = 1 - playerIndex # alternatively 1 or 0
        player = players[playerIndex] # This is 'O' or 'X'
        move = raw_input("You are %s. Enter a lettered row to make a move! " % player)
        while True:
            # Inner loop: we iterate until valid input
            move = move.strip() # remove trailing white space
            try:
                if move == 'q':
                    # player entered 'q' to exit
                    raise SystemExit
                column = "abcdefg".index(move)
                row = find_row(column)
                break # we exit inner loop
            except ValueError:
                move = raw_input("Invalid input, try again: ")
        pmove(player, row, column)

def find_row(column):
    for stuff in range(7):
        if rows[stuff][column] == ' ':
            return stuff
    raise ValueError

def pmove(player, row, column):
    rows[row][column] = player
main()


def win_combos():
    if rows[0][0] and rows[0][1] and rows[0][2] and rows[0][3]=='X':
        return "X Wins"
    elif rows[0][1] and rows[0][2] and rows[0][3] and rows[0][4]=='X':
        return "X Wins"
    elif rows[0][2] and rows[0][3] and rows[0][4] and rows[0][5]=='X':
        return "X Wins"
    elif rows[1][0] and rows[1][1] and rows[1][2] and rows[1][3]=='X':
        return "X Wins"
    elif rows[1][1] and rows[1][2] and rows[1][3] and rows[1][4]=='X':
        return "X Wins"
    elif rows[1][2] and rows[1][3] and rows[1][4] and rows[1][5]=='X':
        return "X Wins"
    elif rows[2][0] and rows[2][1] and rows[2][2] and rows[2][3]=='X':
        return "X Wins"
    elif rows[2][1] and rows[2][2] and rows[2][3] and rows[2][4]=='X':
        return "X Wins"
    elif rows[2][2] and rows[2][3] and rows[2][4] and rows[2][5]=='X':
        return "X Wins"
    elif rows[3][0] and rows[3][1] and rows[3][2] and rows[3][3]=='X':
        return "X Wins"
    elif rows[3][1] and rows[3][2] and rows[3][3] and rows[3][4]=='X':
        return "X Wins"
    elif rows[3][2] and rows[3][3] and rows[3][4] and rows[3][5]=='X':
        return "X Wins"
    elif rows[4][0] and rows[4][1] and rows[4][2] and rows[4][3]=='X':
        return "X Wins"
    elif rows[4][1] and rows[4][2] and rows[4][3] and rows[4][4]=='X':
        return "X Wins"
    elif rows[4][2] and rows[4][3] and rows[4][4] and rows[4][5]=='X':
        return "X Wins"
    elif rows[5][0] and rows[5][1] and rows[5][2] and rows[5][3]=='X':
        return "X Wins"
    elif rows[5][1] and rows[5][2] and rows[5][3] and rows[5][4]=='X':
        return "X Wins"
    elif rows[5][2] and rows[5][3] and rows[5][4] and rows[5][5]=='X':
        return "X Wins"
    elif rows[6][0] and rows[6][1] and rows[6][2] and rows[6][3]=='X':
        return "X Wins"
    elif rows[6][1] and rows[6][2] and rows[6][3] and rows[6][4]=='X':
        return "X Wins"
    elif rows[6][2] and rows[6][3] and rows[6][4] and rows[6][5]=='X':
        return "X Wins"
    elif rows[0][1] and rows[1][1] and rows[2][1] and rows[3][1]=='X':
        return "X Wins"
    elif rows[1][1] and rows[2][1] and rows[3][1] and rows[4][1]=='X':
        return "X Wins"
    elif rows[2][1] and rows[3][1] and rows[4][1] and rows[5][1]=='X':
        return "X Wins"
    elif rows[0][2] and rows[1][2] and rows[2][2] and rows[3][2]=='X':
        return "X Wins"
    elif rows[1][2] and rows[2][2] and rows[3][2] and rows[4][2]=='X':
        return "X Wins"
    elif rows[2][2] and rows[3][2] and rows[4][2] and rows[5][2]=='X':
        return "X Wins"
    elif rows[0][3] and rows[1][3] and rows[2][3] and rows[3][3]=='X':
        return "X Wins"
    elif rows[1][3] and rows[2][3] and rows[3][3] and rows[4][3]=='X':
        return "X Wins"
    elif rows[2][3] and rows[3][3] and rows[4][3] and rows[5][3]=='X':
        return "X Wins"
    elif rows[0][4] and rows[1][4] and rows[2][4] and rows[3][4]=='X':
        return "X Wins"
    elif rows[1][4] and rows[2][4] and rows[3][4] and rows[4][4]=='X':
        return "X Wins"
    elif rows[2][4] and rows[3][4] and rows[4][4] and rows[5][4]=='X':
        return "X Wins"
    elif rows[0][5] and rows[1][5] and rows[2][5] and rows[3][5]=='X':
        return "X Wins"
    elif rows[1][5] and rows[2][5] and rows[3][5] and rows[4][5]=='X':
        return "X Wins"
    elif rows[2][5] and rows[3][5] and rows[4][5] and rows[5][5]=='X':
        return "X Wins"
    elif rows[0][0] and rows[1][1] and rows[2][2] and rows[3][3]=='X':
        return "X Wins"
    elif rows[1][0] and rows[2][1] and rows[3][2] and rows[4][3]=='X':
        return "X Wins"
    elif rows[2][0] and rows[3][1] and rows[4][2] and rows[5][3]=='X':
        return "X Wins"
    elif rows[3][0] and rows[4][1] and rows[5][2] and rows[6][3]=='X':
        return "X Wins"
    elif rows[0][1] and rows[1][2] and rows[2][3] and rows[3][4]=='X':
        return "X Wins"
    elif rows[1][1] and rows[2][2] and rows[3][3] and rows[4][4]=='X':
        return "X Wins"
    elif rows[2][1] and rows[3][2] and rows[4][3] and rows[5][4]=='X':
        return "X Wins"
    elif rows[3][1] and rows[4][2] and rows[5][3] and rows[6][4]=='X':
        return "X Wins"
    elif rows[0][2] and rows[1][3] and rows[2][4] and rows[3][5]=='X':
        return "X Wins"
    elif rows[1][2] and rows[2][3] and rows[3][4] and rows[4][5]=='X':
        return "X Wins"
    elif rows[2][2] and rows[3][3] and rows[4][4] and rows[5][5]=='X':
        return "X Wins"
    elif rows[3][2] and rows[4][3] and rows[5][4] and rows[6][5]=='X':
        return "X Wins"
    elif rows[6][0] and rows[5][1] and rows[4][2] and rows[3][3]=='X':
        return "X Wins"
    elif rows[5][0] and rows[4][1] and rows[3][2] and rows[2][3]=='X':
        return "X Wins"
    elif rows[4][0] and rows[3][1] and rows[2][2] and rows[1][3]=='X':
        return "X Wins"
    elif rows[3][0] and rows[2][1] and rows[1][2] and rows[0][3]=='X':
        return "X Wins"
    elif rows[6][1] and rows[5][2] and rows[4][3] and rows[3][4]=='X':
        return "X Wins"     
    elif rows[5][1] and rows[4][2] and rows[3][3] and rows[2][4]=='X':
        return "X Wins"     
    elif rows[4][1] and rows[3][2] and rows[2][3] and rows[1][4]=='X':
        return "X Wins"     
    elif rows[3][1] and rows[2][2] and rows[1][3] and rows[0][4]=='X':
        return "X Wins"     
    elif rows[6][2] and rows[5][3] and rows[4][4] and rows[3][5]=='X':
        return "X Wins"     
    elif rows[5][2] and rows[4][3] and rows[3][4] and rows[2][5]=='X':
        return "X Wins"     
    elif rows[4][2] and rows[3][3] and rows[2][4] and rows[1][5]=='X':
        return "X Wins"     
    elif rows[3][2] and rows[2][3] and rows[1][4] and rows[0][5]=='X':
        return "X Wins"
    elif rows[0][0] and rows[0][1] and rows[0][2] and rows[0][3]=='O':
        return "O Wins"
    elif rows[0][1] and rows[0][2] and rows[0][3] and rows[0][4]=='O':
        return "O Wins"
    elif rows[0][2] and rows[0][3] and rows[0][4] and rows[0][5]=='O':
        return "O Wins"
    elif rows[1][0] and rows[1][1] and rows[1][2] and rows[1][3]=='O':
        return "O Wins"
    elif rows[1][1] and rows[1][2] and rows[1][3] and rows[1][4]=='O':
        return "O Wins"
    elif rows[1][2] and rows[1][3] and rows[1][4] and rows[1][5]=='O':
        return "O Wins"
    elif rows[2][0] and rows[2][1] and rows[2][2] and rows[2][3]=='O':
        return "O Wins"
    elif rows[2][1] and rows[2][2] and rows[2][3] and rows[2][4]=='O':
        return "O Wins"
    elif rows[2][2] and rows[2][3] and rows[2][4] and rows[2][5]=='O':
        return "O Wins"
    elif rows[3][0] and rows[3][1] and rows[3][2] and rows[3][3]=='O':
        return "O Wins"
    elif rows[3][1] and rows[3][2] and rows[3][3] and rows[3][4]=='O':
        return "O Wins"
    elif rows[3][2] and rows[3][3] and rows[3][4] and rows[3][5]=='O':
        return "O Wins"
    elif rows[4][0] and rows[4][1] and rows[4][2] and rows[4][3]=='O':
        return "O Wins"
    elif rows[4][1] and rows[4][2] and rows[4][3] and rows[4][4]=='O':
        return "O Wins"
    elif rows[4][2] and rows[4][3] and rows[4][4] and rows[4][5]=='O':
        return "O Wins"
    elif rows[5][0] and rows[5][1] and rows[5][2] and rows[5][3]=='O':
        return "O Wins"
    elif rows[5][1] and rows[5][2] and rows[5][3] and rows[5][4]=='O':
        return "O Wins"
    elif rows[5][2] and rows[5][3] and rows[5][4] and rows[5][5]=='O':
        return "O Wins"
    elif rows[6][0] and rows[6][1] and rows[6][2] and rows[6][3]=='O':
        return "O Wins"
    elif rows[6][1] and rows[6][2] and rows[6][3] and rows[6][4]=='O':
        return "O Wins"
    elif rows[6][2] and rows[6][3] and rows[6][4] and rows[6][5]=='O':
        return "O Wins"
    elif rows[0][1] and rows[1][1] and rows[2][1] and rows[3][1]=='O':
        return "O Wins"
    elif rows[1][1] and rows[2][1] and rows[3][1] and rows[4][1]=='O':
        return "O Wins"
    elif rows[2][1] and rows[3][1] and rows[4][1] and rows[5][1]=='O':
        return "O Wins"
    elif rows[0][2] and rows[1][2] and rows[2][2] and rows[3][2]=='O':
        return "O Wins"
    elif rows[1][2] and rows[2][2] and rows[3][2] and rows[4][2]=='O':
        return "O Wins"
    elif rows[2][2] and rows[3][2] and rows[4][2] and rows[5][2]=='O':
        return "O Wins"
    elif rows[0][3] and rows[1][3] and rows[2][3] and rows[3][3]=='O':
        return "O Wins"
    elif rows[1][3] and rows[2][3] and rows[3][3] and rows[4][3]=='O':
        return "O Wins"
    elif rows[2][3] and rows[3][3] and rows[4][3] and rows[5][3]=='O':
        return "O Wins"
    elif rows[0][4] and rows[1][4] and rows[2][4] and rows[3][4]=='O':
        return "O Wins"
    elif rows[1][4] and rows[2][4] and rows[3][4] and rows[4][4]=='O':
        return "O Wins"
    elif rows[2][4] and rows[3][4] and rows[4][4] and rows[5][4]=='O':
        return "O Wins"
    elif rows[0][5] and rows[1][5] and rows[2][5] and rows[3][5]=='O':
        return "O Wins"
    elif rows[1][5] and rows[2][5] and rows[3][5] and rows[4][5]=='O':
        return "O Wins"
    elif rows[2][5] and rows[3][5] and rows[4][5] and rows[5][5]=='O':
        return "O Wins"
    elif rows[0][0] and rows[1][1] and rows[2][2] and rows[3][3]=='O':
        return "O Wins"
    elif rows[1][0] and rows[2][1] and rows[3][2] and rows[4][3]=='O':
        return "O Wins"
    elif rows[2][0] and rows[3][1] and rows[4][2] and rows[5][3]=='O':
        return "O Wins"
    elif rows[3][0] and rows[4][1] and rows[5][2] and rows[6][3]=='O':
        return "O Wins"
    elif rows[0][1] and rows[1][2] and rows[2][3] and rows[3][4]=='O':
        return "O Wins"
    elif rows[1][1] and rows[2][2] and rows[3][3] and rows[4][4]=='O':
        return "O Wins"
    elif rows[2][1] and rows[3][2] and rows[4][3] and rows[5][4]=='O':
        return "O Wins"
    elif rows[3][1] and rows[4][2] and rows[5][3] and rows[6][4]=='O':
        return "O Wins"
    elif rows[0][2] and rows[1][3] and rows[2][4] and rows[3][5]=='O':
        return "O Wins"
    elif rows[1][2] and rows[2][3] and rows[3][4] and rows[4][5]=='O':
        return "O Wins"
    elif rows[2][2] and rows[3][3] and rows[4][4] and rows[5][5]=='O':
        return "O Wins"
    elif rows[3][2] and rows[4][3] and rows[5][4] and rows[6][5]=='O':
        return "O Wins"
    elif rows[6][0] and rows[5][1] and rows[4][2] and rows[3][3]=='O':
        return "O Wins"
    elif rows[5][0] and rows[4][1] and rows[3][2] and rows[2][3]=='O':
        return "O Wins"
    elif rows[4][0] and rows[3][1] and rows[2][2] and rows[1][3]=='O':
        return "O Wins"
    elif rows[3][0] and rows[2][1] and rows[1][2] and rows[0][3]=='O':
        return "O Wins"
    elif rows[6][1] and rows[5][2] and rows[4][3] and rows[3][4]=='O':
        return "O Wins"     
    elif rows[5][1] and rows[4][2] and rows[3][3] and rows[2][4]=='O':
        return "O Wins"     
    elif rows[4][1] and rows[3][2] and rows[2][3] and rows[1][4]=='O':
        return "O Wins"     
    elif rows[3][1] and rows[2][2] and rows[1][3] and rows[0][4]=='O':
        return "O Wins"     
    elif rows[6][2] and rows[5][3] and rows[4][4] and rows[3][5]=='O':
        return "O Wins"     
    elif rows[5][2] and rows[4][3] and rows[3][4] and rows[2][5]=='O':
        return "O Wins"     
    elif rows[4][2] and rows[3][3] and rows[2][4] and rows[1][5]=='O':
        return "O Wins"     
    elif rows[3][2] and rows[2][3] and rows[1][4] and rows[0][5]=='O':
        return "O Wins"

The call to main() in line 45 should go at the end of the program, otherwise, the game starts before the interpreter reads the win_combo function. Also you should rewrite entirely this function with loops. It should take no more than 20 lines.

elif rows[4][2] and rows[3][3] and rows[2][4] and rows[1][5]=='O':
        return "O Wins"

As already stated, you must move the call to main() to the end of your program; however the above syntax is WRONG.

It should be:

elif rows[4][2] == 'O' and rows[3][3] == 'O' and rows[2][2] == 'O' and rows[1][5] == 'O':

As already stated, you must move the call to main() to the end of your program; however the above syntax is WRONG.

It should be:

elif rows[4][2] == 'O' and rows[3][3] == 'O' and rows[2][2] == 'O' and rows[1][5] == 'O':

thanks for telling me this but I have already figured that out, I just dont know where to put the checking code of the function.

if win_combos()=='X Wins':
            print 'test x'
        elif win_combos()=='O Wins':
            print 'test o'

You can place that check at the end of your while loop only add a break to each case so that if either X or O wins, the while loop is ended.

EDIT: Sorry wrong page i posted on.. missed where we were up to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.