Search code examples
pythontic-tac-toe

Python defining functions in Tic Tac Toe


I'm in the early stages of writing a simple Tic Tac Toe game in python. I have defined 3 functions one that initializes the game, one that draws the board and one that asks if player one wants to be X or O. I feel to the best of my knowledge my functions are being requested sequentially and in proper order yet I cannot get the program to move past the first input section. Any help would be amazing.

def start():
    print("Do you want to play Tic Tac Toe? Type yes or no.")
    choice = input()
    while choice == ('yes','no'):
        if choice.lower == ('yes'):
           player_choice()
        elif choice.lower == ('no'):
           print("goodbye")
           quit()
        else:
              print("That was not a valid response. Type yes or no.")
start()

def drawsmall():
    a = (' ___' *  3 )
    b = '   '.join('||||')
    print('\n'.join((a, b, a, b, a, b, a, )))



def player_choice():
    print("Player one it's time to choose, X or O")
    select= input()
    if select.lower ==("x","o"):
        print("Let the game begin.")
        drawsmall()
    elif select.lower != ('x','o'):
        print("Please choose either X or O.")
    else:
        print("Come back when you're done messing around.")
        quit()

Solution

  • Well, after figuring out what you intended, I have seen a number of things that must be changed.

    First, try this and take a look:

    def start():
      print("Do you want to play Tic Tac Toe? Type yes or no.")
      while True:
        choice = input()
        if choice.lower() == 'yes':
          player_choice()
        elif choice.lower() == 'no':
          print("goodbye")
          quit()
        else:
          print("That was not a valid response. Type yes or no.")
    
    
    def drawsmall():
      a = (' ___' * 3)
      b = '   '.join('||||')
      print('\n'.join((a, b, a, b, a, b, a,)))
    
    
    def player_choice():
      print("Player one it's time to choose, X or O")
      select = input()
      if select.lower() in ("x", "o"):
        print("Let the game begin.")
        drawsmall()
      else:
        print("Please choose either X or O.")
    
    start()
    
    1. .lower, should be .lower()
    2. start() must go at the end, not in the middle of the program (otherwise it will not find other functions like player_choice()
    3. a string is not ('string'), you have to remove the parenthesis
    4. if you want to compare a variable with several values, you have to use in, not ==
    5. It's going to be hard to draw the 'X' and 'O' selected by the players when using the stdout!!!