저는 파이썬으로 초보자입니다. 터미널에서 두 사람의 tic tac toe 게임을 만들고 있습니다. 기본적으로이 게임에는 모든 버그와 문제가 해결되었지만 마지막 문제가 하나 있습니다. 기본적으로 이동을 입력하라는 메시지가 나타나면 한 사용자가 어딘가에 이동하라는 메시지가 표시 될 때 문자를 입력하면 문자 또는 비 정수가 입력되면 충돌합니다. 여기에 코드가 있습니다. 그리고 나서 게임이 실행될 때 출력을 넣을 것이고, 사용자는 이동을 요구 한 후 문자를 입력 할 것입니다. OUTPUT파이썬 tic tac 발가락 입력 오류
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
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()
는하지-intiger가 입력되면 적절한 조치를 넣을 때까지, 그것은 (다시 이동에 대한 프롬프트를 줄 것이다, 그래서이 문제를 해결 할 수 있나요 에서). 가능하다면 어떻게 될까요?