Search code examples
pythontic-tac-toe

It repeats the first question for some reason


tic_row1=[7,8,9]
tic_row2=[4,5,6]
tic_row3=[1,2,3]

# Display the grid
def grid_display():
    print(tic_row1)
    print(tic_row2)
    print(tic_row3)

def choice_check():
    
   
    #Choose a plot
    plot='pick'
    number_range= range(0,10)
    number_check=False

    while plot.isdigit()==False or number_check==False:
        plot=input('Please pick a plot number (1-9): ')

        if plot.isdigit()==False:
            plot=input("That isn't a number!")
        if plot.isdigit()==True:
            if int(plot) in number_range:
                number_check=True
            else:
                print("The number must be from (1-9)!")
                number_check=False
    return int(plot)



def game_initiate():
    plot=choice_check()
    team='cookie'
    index=None
    while team != 'x' or 'o':
        team=input("Choose 'X' or 'O': ")

        if team.islower() == 'x' or 'o':
            if plot in range(7,10):
                index=tic_row1.index(plot)
                tic_row1[index]=team
                return tic_row1
            elif plot in range(4,7):
                index=tic_row2.index(plot)
                tic_row2[index]=team
                return tic_row2
            if plot in range(1,4):
                index=tic_row3.index(plot)
                tic_row3[index]=team
                return tic_row3

def game_order():
    nums=range(1,10)
    while nums in tic_row1 or tic_row2 or tic_row3:
        grid_display()
        choice_check()
        game_initiate()

game_order()

So I made this tic-tac-toe game and ultimately it works. For some reason, it asks the sentence 'Please pick a plot number (1-9):' twice. The problem would be in the 'choice_check' function, the second function under the print grids. Maybe I'm just blind, but I can't think of why it's doing this. Everything looks good to my beginner eyes.


Solution

  • For those that want to copy the fixed version. Thanks again to the ones that highlighted what I over-read and missed many times.

    tic_row1=[7,8,9]
    tic_row2=[4,5,6]
    tic_row3=[1,2,3]
    
    # Display the grid
    def grid_display():
        print(tic_row1)
        print(tic_row2)
        print(tic_row3)
    
    def choice_check():
        
       
        #Choose a plot
        plot='pick'
        number_range= range(0,10)
        number_check=False
    
        while plot.isdigit()==False or number_check==False:
            plot=input('Please pick a plot number (1-9): ')
    
            if plot.isdigit()==False:
                plot=input("That isn't a number!")
            if plot.isdigit()==True:
                if int(plot) in number_range:
                    number_check=True
                else:
                    print("The number must be from (1-9)!")
                    number_check=False
        return int(plot)
    
    
    
    def game_initiate():
        plot=choice_check()
        team='cookie'
        index=None
        while team != 'x' or team != 'o':
            team=input("Choose 'X' or 'O': ")
    
            if team.islower() in {'x','o'}:
                if plot in range(7,10):
                    index=tic_row1.index(plot)
                    tic_row1[index]=team
                    return tic_row1
                elif plot in range(4,7):
                    index=tic_row2.index(plot)
                    tic_row2[index]=team
                    return tic_row2
                if plot in range(1,4):
                    index=tic_row3.index(plot)
                    tic_row3[index]=team
                    return tic_row3
    
    def game_order():
        nums=range(1,10)
        while nums in tic_row1 or tic_row2 or tic_row3:
            grid_display()
            game_initiate()
    
    game_order()