tcotto 0 Newbie Poster

So I have this assignment where we have to create connect 4 using classes and make it so you can play against a player or computer. The problem I'm having is returning to the function if the user enters something other than one or two. I know I have to use a if-elif -else structure but I been having trouble using it. Also when playing when the user enter a number outside 1-7 like if i enter -4 it'll play in col 3. Thanks in advance for the help!

import random
class Rack(object):
    def __init__(self, cols, rows):
        self.rows = rows
        self.cols = cols  
                
grid = Rack(7,6)

def returnStartRack(xLen,yLen, blank):
    
    grid = []
    for y in range(yLen):
        row = []
        for x in range(xLen):
            row.append(blank)
            
        grid.append(row)
        
    return grid


def returnUpdatedRack(rowChoice,grid,ch,blank):
    for i in range(5,-1,-1):
        if grid[i][rowChoice] == blank:
            grid[i][rowChoice] = ch
            return grid
        if i<1:
            print "---------COLUMN IS FULL-----------"
            return grid

def checkGameWin(grid):
    whoWon = False
    #Row horizontal
    for row in grid:
        rowStr = ''
        for i in row:
            rowStr += i
        if 'RRRR' in rowStr:
            whoWon = 'player1'
        if 'BBBB' in rowStr:
            whoWon = 'player2'

    #Row verticle
    col = 0
    while col < len(grid[0]):
        colStr = ''
        for row in grid:
            colStr += row[col]
        if 'RRRR' in colStr:
            whoWon = 'player1'
        if 'BBBB' in colStr:
            whoWon = 'player2'
        col+=1

     #Row diagonal moving up right
    for y in range(3,6):
        x = 0
        diag = ''
        while y >= 0 and x <= 6:
            diag += grid[y][x]
            x += 1
            y -= 1
        if 'RRRR' in diag:
            whoWon = 'player1'
        if 'BBBB' in diag:
            whoWon = 'player2'
   
    for x in range(1,4):
        y = 5
        diag = ''
        while y >= 0 and x <= 6:
            diag += grid[y][x]
            x+=1
            y-=1
        if 'RRRR' in diag:
            whoWon = 'player1'
        if 'BBBB' in diag:
            whoWon = 'player2'


    #Row diagonal moving up left
   
    for y in range(3,6):
        x = 6
        diag = ''
        while y <= 5 and x >= 0:
            diag += grid[y][x]
            x -= 1
            y -= 1
        if 'RRRR' in diag:
            whoWon = 'player1'
        if 'BBBB' in diag:
            whoWon = 'player2'
           
    for x in range(5,-1,-1):
        y = 5
        diag = ''
        while y >= 0 and x >= 0:
            diag += grid[y][x]
            x-=1
            y-=1
        if 'RRRR' in diag:
            whoWon = 'player1'
        if 'BBBB' in diag:
            whoWon = 'player2'
           
    return whoWon


p1 = 'R' 
p2 = 'B' 
blank = '-' 

grid=returnStartRack(7,6,blank)
 
player1Win = False
player2Win = False

print '\n1 2 3 4 5 6 7\n'
for y in grid:
    row = ''
    for x in y:
        row += x + ' '
    print row
print '1 2 3 4 5 6 7\n'
a1 = int(raw_input('Enter 2 for player vs computer or 1 for player1 vs player2:'))
if (a1==1):
    
    while player1Win != True and player2Win != True:
           
        p1Choice = int(raw_input('Enter Red Player move,choose 1-7: ')) - 1
        grid = returnUpdatedRack(p1Choice,grid,p1,blank)
        
    
        print '\n1 2 3 4 5 6 7\n'
        for y in grid:
            row = ''
            for x in y:
                row += x + ' '
            print row
        print '\n1 2 3 4 5 6 7\n'
           
        didGameEnd = checkGameWin(grid)
        if didGameEnd == 'player1':
            print '\n Red wins Yayyy!!!'
            player1Win = True
        if didGameEnd == 'player2':
            print '\n Black wins Yuppi!!!'
            player2Win = True
           
        if player1Win != True and player2Win != True:
            p2Choice = int(raw_input('Enter Black Player move(B) choose 1-7: ')) - 1
            grid = returnUpdatedRack(p2Choice,grid,p2,blank)
           
            print '\n1 2 3 4 5 6 7\n'
            for y in grid:
                row = ''
                for x in y:
                    row += x + ' '
                print row
            print '\n1 2 3 4 5 6 7\n'
           
            didGameEnd = checkGameWin(grid)
            if didGameEnd == 'player1':
                print '\nRed wins Yayyy!!!'
                player1Win = True
            if didGameEnd == 'player2':
                print '\nBlack wins Yuppi!!!'
                player2Win = True

        
elif (a1==2):
    while player1Win != True and player2Win != True:
           
        p1Choice = int(raw_input('Enter Red Player move,choose 1-7: ')) - 1
        grid = returnUpdatedRack(p1Choice,grid,p1,blank)
       
        print '\n1 2 3 4 5 6 7\n'
        for y in grid:
            row = ''
            for x in y:
                row += x + ' '
            print row
        print '\n1 2 3 4 5 6 7\n'
           
        didGameEnd = checkGameWin(grid)
        if didGameEnd == 'player1':
            print '\n Red wins Yayyy!!!'
            player1Win = True
        if didGameEnd == 'player2':
            print '\n Black wins Yuppi!!!'
            player2Win = True
           
        if player1Win != True and player2Win != True:

            
            


                num = random.randint(0,7)
                p2Choice = num - 1
                grid = returnUpdatedRack(p2Choice,grid,p2,blank)
           
                print '\n1 2 3 4 5 6 7\n'
                for y in grid:
                    row = ''
                    for x in y:
                        row += x + ' '
                    print row
                print '\n1 2 3 4 5 6 7\n'
           
                didGameEnd = checkGameWin(grid)
                if didGameEnd == 'player1':
                    print '\nRed wins Yayyy!!!'
                    player1Win = True
                if didGameEnd == 'player2':
                    print '\nBlack wins Yuppi!!!'
                    player2Win = True
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.