John_MiracleWorker
John_MiracleWorker

Reputation: 13

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()

Upvotes: 0

Views: 266

Answers (2)

David
David

Reputation: 8298

First, your problem is that you call the lower method wrong. you should call it like the following:

str = 'Test'
print(str.lower())
print(str.lower)
>> test
>> <built-in method lower of str object at 0x7ff42c83ebb0>

fix this issue and you will enter to the right conditions

Second you should change your while loop in the start() like the following:

def start():
    print("Do you want to play Tic Tac Toe? Type yes or no.")
    choice = ‘’
    while choice not in ['yes','no']:
        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.")

notice the if you won't set choice = '' you will not enter The loop.

Third, you should move the calling to the start() function to the end of all the function deceleration in order for them all to be properly recognized.

Note

correct the player_choice() with the logic I provided you in my start()

Upvotes: 1

Kalma
Kalma

Reputation: 147

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!!!

Upvotes: 1

Related Questions