Skip to content

Commit 980aaf5

Browse files
Merge pull request geekcomputers#327 from Anubhav-Bhargava/master
Made code compatible with Python 2 & 3
2 parents 296bd4a + 7afe795 commit 980aaf5

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

TicTacToe.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# Tic Tac Toe
22

33
import random
4+
import sys
5+
6+
def get_input():
7+
if sys.version_info >= (3, 0):
8+
return input()
9+
else:
10+
return raw_input()
411

512
def drawBoard(board):
613
# This function prints out the board that it was passed.
@@ -18,7 +25,7 @@ def inputPlayerLetter():
1825
letter = ''
1926
while not (letter == 'X' or letter == 'O'):
2027
print('Do you want to be X or O?')
21-
letter = input().upper()
28+
letter = get_input().upper()
2229

2330
# the first element in the tuple is the player's letter, the second is the computer's letter.
2431
if letter == 'X':
@@ -36,7 +43,7 @@ def whoGoesFirst():
3643
def playAgain():
3744
# This function returns True if the player wants to play again, otherwise it returns False.
3845
print('Do you want to play again? (yes or no)')
39-
return input().lower().startswith('y')
46+
return get_input().lower().startswith('y')
4047

4148
def makeMove(board, letter, move):
4249
if isSpaceFree(board,move):
@@ -74,7 +81,7 @@ def getPlayerMove(board):
7481
move = ' '
7582
while move not in '1 2 3 4 5 6 7 8 9'.split() or not isSpaceFree(board, int(move)):
7683
print('What is your next move? (1-9)')
77-
move = input()
84+
move = get_input()
7885
return int(move)
7986

8087
def chooseRandomMoveFromList(board, movesList):
@@ -186,4 +193,4 @@ def main():
186193
break
187194

188195
if __name__ == "__main__":
189-
main()
196+
main()

0 commit comments

Comments
 (0)