I am a beginner with Python, and I am creating a two-player tic-tac-toe game in the terminal.
Basically, this game has all its bugs and kinks worked out, however, I have one last problem. Basically, when prompted to enter a move, if one user enters a letter when prompted for a move to go somewhere, if a letter or non-integer is inputted, it crashes. Here is the code, and then I will put the output when a game is ran, and a user inputs a letter after the prompt for a move.
X = "X"
O = "O"
empty = " "
S = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
def Instructions():
print "Fill in spaces on the board with number corresponding to the board below."
print ""
print "",1,"|",2,"|",3
print "","---------"
print "",4,"|",5,"|",6
print "","---------"
print "",7,"|",8,"|",9
print ""
def Board():
print ""
print "",S[0],"|",S[1],"|",S[2]
print "","---------"
print "",S[3],"|",S[4],"|",S[5]
print "","---------"
print "",S[6],"|",S[7],"|",S[8], "\n"
def WhoGoesFirst():
Instructions()
global order
letter = raw_input('Who goes first, X or O? ').upper()
while not (letter == "X" or letter == "O"):
letter = raw_input('Who goes first, X or O? ').upper()
if letter == "X":
order = [X, O, X, O, X, O, X, O, X]
else:
order = [O, X, O, X, O, X, O, X, O]
def CheckWin():
global winner
winner = ""
if S[0] == S[1] == S[2] != empty:
winner = S[0]
if S[3] == S[4] == S[5] != empty:
winner = S[3]
if S[6] == S[7] == S[8] != empty:
winner = S[6]
if S[0] == S[3] == S[6] != empty:
winner = S[0]
if S[1] == S[4] == S[7] != empty:
winner = S[1]
if S[2] == S[5] == S[8] != empty:
winner = S[2]
if S[0] == S[4] == S[8] != empty:
winner = S[0]
if S[2] == S[4] == S[6] != empty:
winner = S[2]
def Move(turn):
move = input('Choose a Space from 1-9 for ' + str(order[turn]) + ' to Go: ')
while move not in range (1, 10) or S[int(move) - 1] is not empty:
move = input('Choose a Space from 1-9 for ' + str(order[turn]) + ' to Go: ')
S[int(move) - 1] = order[turn]
Board()
CheckWin()
def MakeMove():
turn = 0
while turn <= 8:
Move(turn)
turn += 1
if winner == X or winner == O:
while turn <= 8:
turn += 1
if winner == X:
print winner + " Is the Winner!"
if winner == O:
print winner + " Is the Winner!"
if winner == "":
print "The Game Is a Tie"
WhoGoesFirst()
MakeMove()
OUTPUT
Fill in spaces on the board with number corresponding to the board below.
1 | 2 | 3
---------
4 | 5 | 6
---------
7 | 8 | 9
Who goes first, X or O? x
Choose a Space from 1-9 for X to Go: 1
X | |
---------
| |
---------
| |
Choose a Space from 1-9 for O to Go: k
Traceback (most recent call last):
File "move.py", line 79, in <module>
MakeMove()
File "move.py", line 66, in MakeMove
Move(turn)
File "move.py", line 56, in Move
move = input('Choose a Space from 1-9 for ' + str(order[turn]) + ' to Go: ')
File "<string>", line 1, in <module>
NameError: name 'k' is not defined
Is it possible to fix this problem, so that if a non-integer is inputted, it will give the prompt for a move again, (until a proper move is put in). If it is possible, then how would it be done?
To fix your immediate problem (under Python 2), use raw_input
instead of input
.
Under Python 2, input
will get a value from you then try to evaluate it. Evaluating 1
is okay but evaluating k
when there's no such variable is not.
Once you have the string, you can check it however you like before attempting to convert it to an integer, such as with:
def Move(turn):
move = -1
while move not in range (1, 10) or S[int(move) - 1] is not empty:
smove = raw_input('Choose a Space from 1-9 for ' + str(order[turn]) + ' to Go: ')
try:
move = int(smove)
except ValueError:
move = -1
S[int(move) - 1] = order[turn]
Board()
CheckWin()
Python 3 is also a fix to this problem since its input
function is equivalent to the raw_input
of Python 2. In any case, development of Python2 has now stopped and people should be moving their stuff to Python 3 unless they have a very good reason.