我是python的初学者,我正在终端创建一个双人tic tac toe游戏。基本上,这个游戏有它的所有错误和纠结,但是,我有一个最后的问题。基本上,当提示输入移动时,如果一个用户在提示移动到某处时输入一个字母,如果输入了一个字母或非整数,它就会崩溃。这是代码,然后我会在游戏运行时输出输出,并且用户在提示移动后输入一个字母。
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()
输出
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
是否有可能解决这个问题,因此如果输入了一个not-intiger,它将再次提示移动,(直到放入适当的移动)。如果有可能,那么它将如何完成。
答案 0 :(得分:1)
是的,要解决立即问题,请使用raw_input
。
在Python 2下,input
会从您那里获得一个值,然后尝试对其进行评估。评估1
是可以的,但在没有这样的情况下评估k
变量不是。
一旦你有了字符串,你可以在尝试将它转换为整数之前检查它,例如:
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也是一个修复,因为它的input
函数等同于Pyhon 2的raw_input
。
答案 1 :(得分:0)