(Python)Idk做什么

时间:2016-05-01 02:39:13

标签: python

所以我正在学习如何从一个网站制作一个tic tac toe游戏,当我完成时,我无法播放它,因为我一直得到同样的错误,我检查了我的工作,一切都很好,这里和#39;是代码

import random
from time import sleep
print "To play this game, you use your 1-9 keys"
print "This is layout of each number representing a square"
print "7|8|9"
print "4|5|6"
print "1|2|3"
print " "
def drawboard(board): #Draws the board
    print "Board"
    print '' + board[7] + '|' + board[8] + '|' + board[9]
    print '' + board[4] + '|' + board[5] + '|' + board[6]
    print '' + board[1] + '|' + board[2] + '|' + board[3]

def inputplayerletter(): #Asks player if they want to be X or O
    letter = ''
    while not letter == 'X' or letter == 'O':
        print "Do you want to be X or O?"
        letter = raw_input(" ").upper()
    if letter == 'X':
        return ['X', 'O']
    else:
        return ['O', 'X']

def whogoesfirst(): #This determines who goes first
    if random.randint(0,1) == '0':
        return 'computer'
    else:
        return 'player'

def playagain(): #Asks player if it wants to play again
    print "Do you want to play again?(Y/N)"
    return raw_input("").lower().startswith('y')

def makemove(board, letter, move): #When someone makes a move we execute this function
    board[move] = letter

def winner(bo,le): #Determines whose the winner
    return ((bo[7] == le and bo[8] == le and bo[9] == le) or
    (bo[4] == le and bo[5] == le and bo[6] == le) or
    (bo[1] == le and bo[2] == le and bo[3] == le) or
    (bo[7] == le and bo[4] == le and bo[1] == le) or
    (bo[8] == le and bo[5] == le and bo[2] == le) or
    (bo[9] == le and bo[6] == le and bo[3] == le) or
    (bo[7] == le and bo[5] == le and bo[3] == le) or
    (bo[9] == le and bo[5] == le and bo[1] == le)


def getboardcopy(board): #Creates a copy of the board for the computer AI
    dupeboard = []
    for i in dupeboard:
        dupeboard.append(i)
    return dupeboard

def isspacefree(board, move): #Checks if space is free.
    return board[move] == ''

def playermove(board): #Where the player inputs his/her move
    move = ''
    while move not in '1 2 3 4 5 6 7 8 9'.split() or not isspacefree(board, int(move)):
        print "your move (Pick from 1-9)"
        move = raw_input("")
    return int(move)

def choosemovefromlist(board, movelist): #chooses possible moves from list
    possiblemoves = []
    for i in moveslist:
        if isspacefree(board, i):
            possiblemoves.append(i)
    if len(possiblemoves) != 0:
        return random.choice(possiblemove)
    else:
        return None

def computerai(board, computerletter): #Computer AI
    if computerletter == 'X':
        playerletter = 'O'
    else:
        playerletter = 'X'

    for i in range(1,10):
        dupe = getboardcopy(board)
        if isspacefree(copy, i):
            makemove(board, computerletter, i)
            if winner(copy, computerletter):
                return i
    for i in range(1,10):
        dupe = getboardcp[y(board)
        if isspacefree(copy, i):
            makemove(board, playerletter, i)
            if winner(copy, playerletter):
                return i
    move = choosemovefromlist(board, [1, 3, 7, 9])
    if move != None:
        return move
    if isspacefree(board, 5):
        return 5
    return choosemovefromlist(board[2,4,6,8])

def isboardfull(board):
    for i in range(1,10):
        if isspacefree(board, i):
            return False
    return True

print "Welcome to Tic Tac Toe!"

while True:
    theboard = [''] * 10
    print "Flipping the coin..."
    sleep(2)
    turn = whogoesfirst()
    print "The " + turn + " goes first."
    gameinprogress = True
    while gameinprogress:
        if turn == 'player':
            drawboard(theboard)
            move = getplayermove(theboard)
            makemove = (theboard, playerletter, move)
            if winner(theboard, playerletter):
                drawboard(theboard)
                print "The player wins the game! Congratulations!"
                gameinprogress = False
            else:
                if isboardfull(theboard):
                    drawboard(theboard)
                    print "Tie game! No one wins!"
                    break
                else:
                    turn = 'computer'
        else:
            move = computerai(theboard, computerletter)
            makemove(theboard, computerletter, move)
            if winner(theboard, computerletter):
                drawboard(theboard)
                print "The computer has beaten the player!"
                break
            else:
                turn = 'player'
    if not playagain():
        break

我一直在收到错误:

def getboardcopy(board): #Creates a copy of the board for the computer AI
    dupeboard = []
    for i in dupeboard:
        dupeboard.append(i)
    return dupeboard

def isspacefree(board, move): #Checks if space is free.
    return board[move] == ''

def playermove(board): #Where the player inputs his/her move
    move = ''
    while move not in '1 2 3 4 5 6 7 8 9'.split() or not isspacefree(board, int(move)):
        print "your move (Pick from 1-9)"
        move = raw_input("")
    return int(move)

def choosemovefromlist(board, movelist): #chooses possible moves from list
    possiblemoves = []
    for i in moveslist:
        if isspacefree(board, i):
            possiblemoves.append(i)
    if len(possiblemoves) != 0:
        return random.choice(possiblemove)
    else:
        return None

def computerai(board, computerletter): #Computer AI
    if computerletter == 'X':
        playerletter = 'O'
    else:
        playerletter = 'X'

    for i in range(1,10):
        dupe = getboardcopy(board)
        if isspacefree(copy, i):
            makemove(board, computerletter, i)
            if winner(copy, computerletter):
                return i
    for i in range(1,10):
        dupe = getboardcp[y(board)
        if isspacefree(copy, i):
            makemove(board, playerletter, i)
            if winner(copy, playerletter):
                return i
    move = choosemovefromlist(board, [1, 3, 7, 9])
    if move != None:
        return move
    if isspacefree(board, 5):
        return 5
    return choosemovefromlist(board[2,4,6,8])

def isboardfull(board):
    for i in range(1,10):
        if isspacefree(board, i):
            return False
    return True

print "Welcome to Tic Tac Toe!"

即使我在此删除已定义的函数,也会出现另一个错误。

因此,当我正常运行代码时,这就是我得到的错误

Traceback (most recent call last):
  File "python", line 49
    def getboardcopy(board): #Creates a copy of the board for the computer AI
      ^
SyntaxError: invalid syntax

因此,当我从代码中删除定义的函数时,我收到此错误:

Traceback (most recent call last):
  File "python", line 50
    def isspacefree(board, move): #Checks if space is free.
      ^
SyntaxError: invalid syntax

即使我删除了该函数,在此函数之后函数也会发生同样的错误,然后如果我从def getboardcopy开始删除每个函数,我的print语句出错。然后当我删除时,我从while循环中得到另一个错误。我使用的是Python 2.7.2

2 个答案:

答案 0 :(得分:1)

即使这应该是关闭的,这也是一个很好的学习机会。当你得到那种类型的级联错误时,通常意味着错误信息之上的某些东西是不平衡的。

您应该能够分辨的另一种方式是,每次按下回车键时,光标都不会达到您预期的结束位置。

所以指出的错误是

def winner(bo,le): #Determines whose the winner
    return ((bo[7] == le and bo[8] == le and bo[9] == le) or
           (bo[4] == le and bo[5] == le and bo[6] == le) or
           (bo[1] == le and bo[2] == le and bo[3] == le) or
           (bo[7] == le and bo[4] == le and bo[1] == le) or
           (bo[8] == le and bo[5] == le and bo[2] == le) or
           (bo[9] == le and bo[6] == le and bo[3] == le) or
           (bo[7] == le and bo[5] == le and bo[3] == le) or
           (bo[9] == le and bo[5] == le and bo[1] == le)) <- this was missing

答案 1 :(得分:0)

存在大量语法错误并调用不存在的变量。该程序也在主while循环中缺少对inputplayerletter()的调用。它现在将运行,但您需要修复仍然存在的所有逻辑问题。

@ControllerAdvice
public class RequestExceptionHandler extends ResponseEntityExceptionHandler {

  @Override
  protected ResponseEntity<Object> handleMissingServletRequestPart(MissingServletRequestPartException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
    MyCustomErrorResource error = new MyCustomErrorResource("missing parameter");
    return handleExceptionInternal(ex, error, headers, status, request);
  }
}