简体   繁体   English

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

[英]Receiving "TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'" when attempting to replace item in a list

I am trying to create a tic-tac-toe game and keep running into this error when trying to replace an item in the board list with an 'X'.我正在尝试创建井字游戏,并在尝试用“X”替换棋盘列表中的项目时不断遇到此错误。 This is my code:这是我的代码:

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.... > ")

This is where the space is being replaced with an 'X'这是空格被替换为“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()

The error code:错误代码:

    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'

You can modify code like this.您可以像这样修改代码。

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)

This happens, in your function placement, you are trying to transform an input to int().发生这种情况时,在您的函数放置中,您试图将输入转换为 int()。 The input from the user is stored in the variable: pos and you need to figure out some way to get this value into your function placement.来自用户的输入存储在变量:pos 中,您需要想办法将此值放入您的函数位置。

This could look like this:这可能是这样的:

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

Where you would call this function from your turns function like this:你可以从你的 turn 函数中调用这个函数,如下所示:

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

I also made a small change to how your code runs:我还对您的代码运行方式做了一些小改动:

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

Off course this should later be changed to while game_not_finished(): where you would check if the game is finished or not.当然,稍后应该将其更改为 while game_not_finished():您将在其中检查游戏是否已完成。

Hope this helps and good luck improving the game from this point!希望这会有所帮助,并祝您好运从这一点改进游戏!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 int()参数必须是字符串,类似字节的对象或数字,而不是“列表” - int() argument must be a string, a bytes-like object or a number, not 'list' Django TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是&#39;list&#39; - Django TypeError: int() argument must be a string, a bytes-like object or a number, not 'list' 类型错误:int() 参数必须是字符串、类似字节的对象或数字,而不是“列表” - TypeError: int() argument must be a string, a bytes-like object or a number, not 'list' Python 2-TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是“列表” - Python 2 - TypeError: int() argument must be a string, a bytes-like object or a number, not 'list' TypeError at /students/exam/1/ int() 参数必须是字符串、类似字节的对象或数字,而不是“列表” - TypeError at /students/exam/1/ int() argument must be a string, a bytes-like object or a number, not 'list' 类型错误:int() 参数必须是字符串、类似字节的对象或数字,而不是“列表”? - TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'? 如何在python中将图像列表转换为数字列表? TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是“ Image” - How to convert list of images into list of numbers in python? TypeError: int() argument must be a string, a bytes-like object or a number, not 'Image' Python Pandas: Using a map function within a lambda / TypeError: (“int() argument must be a string, a bytes-like object or a number, not 'list'” - Python Pandas: Using a map function within a lambda / TypeError: (“int() argument must be a string, a bytes-like object or a number, not 'list'” 命令行 - int()参数必须是字符串,类似字节的对象或数字,而不是&#39;list&#39; - command line - int() argument must be a string, a bytes-like object or a number, not 'list' int()参数必须是字符串,类似字节的对象或数字,而不是&#39;list&#39;代码错误 - int() argument must be a string, a bytes-like object or a number, not 'list' Error in code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM