尝试替换列表中的项目时,收到“ TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是'list'”

问题描述 投票:-1回答:2

我正在尝试创建井字游戏,并在尝试将棋盘列表中的项目替换为'X'时继续遇到此错误。这是我的代码:

board = ["_", "_", "_",
         "_", "_", "_",
         "_", "_", "_"]


def display_board():
    print(board[0] + " | " + board[1] + " | " + board[2])
    print(board[3] + " | " + board[4] + " | " + board[5])
    print(board[6] + " | " + board[7] + " | " + board[8])


def play():
    print("----------------------------")
    print("~~~ T I C  T A C  T O E ~~~ ")
    print("----------------------------")
    print("")
    print("")
    play_option = input("Would you like to play? 1 for 'Yes' and 2 for 'No' > ")


    if int(play_option) ==1:
        print("")
        print("")
        display_board()
    else:
     print("")
     print("Okay, Bye!")


def turns():
    pos = input("Where would you like to place? EX. 1, 2, 3.... > ")

这是用'X'代替空格的地方

def placement():
    if int(input) == 1:
        board[0] = "X"
        display_board()
    elif int(input) == 2:
        board[1] = "X"
        display_board()
    elif int(input) == 3:
        board[1] = "X"
        display_board()
play()
turns()
placement()

错误代码:

    Traceback (most recent call last):
     File "C:/Users/Administrator/tiktactoe/Tik-Tac-TOe.py", line 51, in <module>
    placement()

    File "C:/Users/Administrator/tiktactoe/Tik-Tac-TOe.py", line 35, in placement
    if int(input) == 1:

    TypeError: int() argument must be a string, a bytes-like object or a number, 
    not 'builtin_function_or_method'
python
2个回答
0
投票

您可以修改这样的代码。

board = ["_", "_", "_",
         "_", "_", "_",
         "_", "_", "_"]


def display_board():
    print(board[0] + " | " + board[1] + " | " + board[2])
    print(board[3] + " | " + board[4] + " | " + board[5])
    print(board[6] + " | " + board[7] + " | " + board[8])


def play():
    print("----------------------------")
    print("~~~ T I C  T A C  T O E ~~~ ")
    print("----------------------------")
    print("")
    print("")
    play_option = input("Would you like to play? 1 for 'Yes' and 2 for 'No' > ")


    if int(play_option) ==1:
        print("")
        print("")
        display_board()
    else:
     print("")
     print("Okay, Bye!")


def turns():
    pos = input("Where would you like to place? EX. 1, 2, 3.... > ")
    return pos

def placement(input):
    if int(input) == 1:
        board[0] = "X"
        display_board()
    elif int(input) == 2:
        board[1] = "X"
        display_board()
    elif int(input) == 3:
        board[1] = "X"
        display_board()

play()
pos = turns()
placement(pos)

0
投票

发生这种情况,在您的函数放置中,您试图将输入转换为int()。用户的输入存储在变量pos中,您需要找出一些方法将此值放入函数位置。

这可能看起来像这样:

def placement(input, type):
    board[int(input) - 1] = type
    display_board()

您将在以下的turns函数中调用此函数的位置:

def turns(type):
    pos = input("Where would you like to place {0}? EX. 1, 2, 3.... > ".format(type))
    placement(pos, type)

我还对代码的运行方式进行了小幅更改:

play()
while True:
    for i in ['X', 'O']:
        turns(i)

当然,稍后应将其更改为game_not_finished():您将在其中检查游戏是否完成。

希望这会有所帮助,祝您好运,从现在开始改善游戏!

© www.soinside.com 2019 - 2024. All rights reserved.