My coursework is to create Tic Tac Toe in Python, my tutor helped me get it working in 2.7 however It needs to be in 3.5.
Firstly in 2.7 the code below prints a 3x3 list, however in 3.5 it just prints the list downwards not 3x3. my tutor said maybe put end = ' '
at the end but that also doesn't work.
def printBoard( board ):
counter = 0
for y in range(3):
for x in range(3):
print (board[counter]),
counter += 1
print
print
second problem is on 2.7 it allows me to continue to input numbers till the board is filled with X or O, on 3.5 it only allows to input once and then the program ends?
value = input("input number between 1 and 9")
value = int(value)
if value == 1:
alist[0] = player1
printBoard( alist )
value = input("input number between 1 and 9")
if value == 2:
alist[1] = player1
printBoard( alist )
value = input("input number between 1 and 9")
etc.
print
changed from statement to a function in Python 3.x. To print a statement without newline, you need to pass end=' '
parameter (You can use the print
as a function in Python 2.7 if you put from __future__ import print_function
at the beginning of the code):
print(board[counter], end=' ')
input
returns a string in Python 3.x. (does not evaluate the input string). You need to convert the value into int
every where you used input
:
value = input("input number between 1 and 9")
value = int(value)
Alternatively, instead of comparing the input with integer literal 1
or 2
, compare the input string with strings: '1'
, '2'
without converting the string into integer. (But this requires you to use raw_input
in Python 2.7 instead of input
)
print
should be called: print()
. Otherwise, nothing is printed.