【问题标题】:I'm getting an error on a ticTacToe game i'm making in python, can't figure out proper solving of this error anywhere我在 python 中制作的 ticTacToe 游戏出现错误,无法在任何地方正确解决此错误
【发布时间】:2017-10-07 05:23:35
【问题描述】:

我正在为我所在的 compSci 课程制作井字游戏,我已经在这个问题上研究了大约一个小时,运气或头脑绝对为零,如果有人能接受,我将非常感激看看它,看看我在哪里愚蠢。 所以每当我运行下面的井字游戏程序时,我都会收到这个错误:

Traceback (most recent call last):
  File     "C:\Users\kolton\Documents\Python\Josh\compSci\projects\games\ticTacToe\ticTacTo    ev3.py", line 125, in <module>
    move = playerMove(playBoard)
  File     "C:\Users\kolton\Documents\Python\Josh\compSci\projects\games\ticTacToe\ticTacTo    ev3.py", line 68, in playerMove
    move = input("Which space would you like to place your ", pLetter, " in?        (1-9)\n Answer:")
TypeError: input expected at most 1 arguments, got 3

这是实际的程序:

import random
def drawBoard(board):
    print("     |     |     ")
    print("  "+board[0]+"  |  "+board[1]+"  |  "+board[2]+"  ")
    print("     |     |     ")
    print("-----------------")
    print("     |     |     ")
    print("  "+board[3]+"  |  "+board[6]+"  |  "+board[5]+"  ")
    print("     |     |     ")
    print("-----------------")
    print("     |     |     ")
    print("  "+board[6]+"  |  "+board[7]+"  |  "+board[8]+"  ")
    print("     |     |     ")

def playerLetter():
    letter=""
    while not (letter=="X" or letter=="O"):
        print("Would you like to be X's or O's?(x/o)\nAnswer: ")
        letter=input().upper()
    if letter=="X":
        return["X", "O"]
    else:
        return["O", "X"]
def whoGoesFirst():
    firstN=random.randrange(1, 2)+1
    if firstN ==1:
        return "computer"
    else:
        return "player"

def playAgain():
    keepPlaying = input("Would you like to play again?(y/n)")
    keepPlaying = keepPlaying.lower()
    if keepPlaying == "y":
        print("Cool!")
        play = "y"
    elif keepPlaying == "n":
        print("Darn, well thanks for playing.")
        play = "n"
    else:
        print("You can only answer with an 'n' or an 'a'")

def move(board, pLetter, move):
    board[move]=pLetter

def won(board, pLetter):
    return ((board[1]==letter and board[2]==letter and board[3]==letter) or
            (board[4]==letter and board[5]==letter and board[6]==letter) or
            (board[7]==letter and board[8]==letter and board[9]==letter) or
            (board[1]==letter and board[4]==letter and board[7]==letter) or
            (board[2]==letter and board[5]==letter and board[8]==letter) or
            (board[3]==letter and board[6]==letter and board[9]==letter) or
            (board[1]==letter and board[5]==letter and board[9]==letter) or
            (board[7]==letter and board[5]==letter and board[3]==letter))

def copyBoard(board):
    boardCopy=[]
    for m in board:
        boardCopy.append(m)
    return boardCopy

def isSpaceFree(board, move):
    return board[move]==" "

def playerMove(board):
    move=""
    while move not in "1 2 3 4 5 6 7 8 9".split() or not isSpaceFree(board, int(move)):
        move = input("Which space would you like to place your ", pLetter,  " in?(1-9)\n Answer:")
    return int(move)

def doRandomMove(board, moveList):
    possibleMoves=[]
    for m in moveList:
        if isSpaceFree(board, i):
            possibleMoves.append(m)
    if len(possibleMoves)!=0:
        return random.choice(possibleMoves)
    else:
        return None

def getCMove(board, cLetter):
    for n in range(1, 10):
        copy = boardCopy(board)
        if isSpaceFree(copy, m):
            makeMove(copy, cLetter, m)
            if won(copy, cLetter):
                return m
    for m in range(1, 10):
        copy = boardCopy(board)
        if isSpaceFree(copy, m):
            makeMove(copy, pLetter, m)
            if won(copy, pLetter):
                return m
    move = chooseRandomMoveFromList(board, [1, 3, 7, 9])
    if move != None:
        return move
    if ifSpaceFree(board, 5):
        return 5
    return chooseRandomMoveFromList(board, [2, 4, 6, 8])

def fullBoard (board):
    for s in range(1, 10):
        if isSpaceFree(board, s):
            return False
    return True

print ("""\n
`7MMF'     A     `7MF'     `7MM                                           
  `MA     ,MA     ,V         MM                                           
   VM:   ,VVM:   ,V .gP"Ya   MM  ,p6"bo   ,pW"Wq.`7MMpMMMb.pMMMb.  .gP"Ya 
    MM.  M' MM.  M',M'   Yb  MM 6M'  OO  6W'   `Wb MM    MM    MM ,M'   Yb
    `MM A'  `MM A' 8M8M8M8M  MM 8M       8M     M8 MM    MM    MM 8M8M8M8M
     :MM;    :MM;  YM.    ,  MM YM.    , YA.   ,A9 MM    MM    MM YM.    ,
      VF      VF    `Mbmmd'.JMML.YMbmd'   `Ybmd9'.JMML  JMML  JMML.`Mbmmd' """)

while True:
    playBoard = [" "] * 10
    pLetter, cLetter = playerLetter()
    turn = whoGoesFirst()
    print("The ", turn, " will go first.")
    gameIsPlaying=True
    while gameIsPlaying == True:
        if turn == "player":
            drawBoard(playBoard)
            move = playerMove(playBoard)
            makeMove(playBoard, pLetter, move)
            if isWinner(playBoard, pLetter):
                drawBoard(playBoard)
                print("Congrats!!!")
                gameIsPlaying = False
            else:
                if fullBoard(playBoard):
                    drawBoard(playBoard)
                    print("The game is a tie.")
                    break
                else:
                    turn = "computer"
        else:
            move = getCMove(theBoard, cLetter)
            move(playBoard, cLetter, move)
            if won(playBoard, cLetter, move):
                drawBoard(playBoard)
                print("The computer won the game.")
                gameIsPlaying = False
            else:
                if fullBoard(playBoard):
                    drawBoard(playBoard)
                    print("It's a tie!")
                    break
                else:
                    turn = "player"

        if not playAgain():
            break    

【问题讨论】:

  • 请阅读input()函数的文档;它与print() 函数的工作方式不同:它不接受随机数量的参数。
  • 您可能希望将标题更改为更短且更具描述性。标题并不意味着是一个段落。

标签: python python-3.x computer-science tic-tac-toe


【解决方案1】:

错误说的很清楚

TypeError: input expected at most 1 arguments, got 3

来自文档 here inputraw_input 仅接受 1 个参数 (prompt)。您应该在使用之前创建一个字符串,或者更好地使用字符串插值。检查 - here 它有很好的例子。

【讨论】:

  • 非常感谢,这么鲁莽,我还是Python新手,真的需要学习更多的基本功能和错误阅读。但是非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多