井字游戏输入错误并在 Python 中崩溃

Tic-tac-toe input error and crash in Python

我是 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

是否可以解决这个问题,如果输入非整数,它会再次给出移动提示,(直到输入正确的移动)。如果可以的话,怎么做到的?

要解决 直接 问题(在 Python 2 下),请使用 raw_input 而不是 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功能等同于[=34=的raw_input ] 2. 无论如何,Python2 的开发现在已经停止,人们应该将他们的东西转移到 Python3,除非他们有 非常 充分的理由。

调用 input 时,值的句法必须正确 python。

This function does not catch user errors. If the input is not syntactically valid, a SyntaxError will be raised. Other exceptions may be raised if there is an error during evaluation

另一方面,raw_input 被认为是一个字符串。

The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.