Python 类型错误:序列项 0:预期的字符串,找到 NoneType

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18852324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:03:48  来源:igfitidea点击:

TypeError: sequence item 0: expected string, NoneType found

pythontypeerror

提问by BLeeM

I'm trying to improve a game of battleships. The original version works fine with no errors. I have written code to help overcome the fact that the first version places the ships in the same place every time so I have started with one ship (made of two squares). I have done this by creating two functions: the first generates a random coordinate...

我正在努力改进战舰游戏。原始版本工作正常,没有错误。我编写了代码来帮助克服第一个版本每次都将船只放在同一个地方的事实,所以我从一艘船(由两个方块组成)开始。我通过创建两个函数来做到这一点:第一个生成一个随机坐标......

 # Destroyer (2 squares)
def Deploy_Destroyer_1(Player):
    rand_col_1 = randint(0,11)
    if rand_col_1 <= 5:
        rand_row_1 = randint(0,11)
    else:
        rand_row_1 = randint(6,11)
    return rand_col_1
    return rand_row_1
    if Player[rand_row_1][rand_col_1] == 'X':
        Deploy_Destroyer_1(Player)
    else:
        Deploy_Destroyer_2(Player)

and the second trials this co-ordinate against the conditions (if it will fit on the board and which rotation it can be placed).

第二次试验是根据条件协调的(如果它适合板上以及它可以放置哪个旋转)。

def Deploy_Destroyer_2(Player):
    if rand_col_1 == 5 and rand_row_1 == 6:
        #can be 1, 2, 3 or 4... in that order below
        rand_position_1 = randint(1,4)
        if rand_position_1 == 1:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 + 1][rand_col_1] = 2
        if rand_position_1 == 2:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 - 1][rand_col_1] = 2
        if rand_position_1 == 3:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 + 1] = 2
        if rand_position_1 == 4:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 - 1] = 2
    elif rand_col_1 in range(1,4) and rand_row_1 in range(1,10):
        #can be any 1, 2, 3 or 4... in that order below
        rand_position_1 = randint(1,4)
        if rand_position_1 == 1:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 + 1][rand_col_1] = 2
        if rand_position_1 == 2:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 - 1][rand_col_1] = 2
        if rand_position_1 == 3:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 + 1] = 2
        if rand_position_1 == 4:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 - 1] = 2
    elif rand_col_1 in range(5,10) and rand_row_1 in range(7,10):
        #can be any 1, 2, 3 or 4... in that order below
        rand_position_1 = randint(1,4)
        if rand_position_1 == 1:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 + 1][rand_col_1] = 2
        if rand_position_1 == 2:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 - 1][rand_col_1] = 2
        if rand_position_1 == 3:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 + 1] = 2
        if rand_position_1 == 4:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 - 1] = 2
    elif rand_col_1 == 0 and rand_row_1 == 0:
        #can be any 1, 2, 3 or 4... in that order below
        rand_position_1 = randint(1,4)
        if rand_position_1 == 1:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 + 1][rand_col_1] = 2
        if rand_position_1 == 2:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 - 1][rand_col_1] = 2
        if rand_position_1 == 3:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 + 1] = 2
        if rand_position_1 == 4:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 - 1] = 2
    elif (rand_col_1 == 5 and rand_row_1 == 0) or (rand_col_1 == 11 and rand_row_1 ==6):
        #can be one or four
        #check brackets and booleans here
        rand_position_1 = randint(1,2)
        if rand_position_1 == 1: #position 1
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 + 1][rand_col_1] = 2
        if rand_position_1 == 2: #position 4
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 - 1] = 2
    elif rand_col_1 == 0 and rand_row_1 == 11:
        #can be 2 or 3
        rand_position_1 = randint(2,3)
        if rand_position_1 == 2: #position 2
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 - 1][rand_col_1] = 2
        if rand_position_1 == 3: #position 3
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 + 1] = 2
    elif rand_col_1 == 11 and rand_row_1 == 11:
        #can be 2 or 4
        rand_position_1 = randint(1,2)
        if rand_position_1 == 1: #position 2
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 - 1][rand_col_1] = 2
        if rand_position_1 == 2: #position 4
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 - 1] = 2
    elif (rand_row_1 == 0 and rand_col_1 in range(1,4)) or (rand_row_1 == 6 and rand_col_1 in range(6,10)):
        #can be 1, 3 or 4
        #check brackets and booleans here
        rand_position_1 = randint(1,3)
        if rand_position_1 == 1: #position 1
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 + 1][rand_col_1] = 2
        if rand_position_1 == 2: #position 3
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 + 1] = 2
        if rand_position_1 == 3: #position 4
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 - 1] = 2
    elif (rand_col_1 == 5 and rand_row_1 in range(1,5)) or (rand_col_1 == 11 and rand_row_1 in range(7,10)):
        #can be 1, 2 or 4
        #check brackets and booleans here
        rand_position_1 = randint(1,3)
        if rand_position_1 == 1: #position 1
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 + 1][rand_col_1] = 2
        if rand_position_1 == 2: #position 2
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 - 1][rand_col_1] = 2
        if rand_position_1 == 3: #position 4
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 - 1] = 2
    elif rand_col_1 == 0 and rand_row_1 in range(1,10):
        #can be 1, 2 or 3... in that order below
        rand_position_1 = randint(1,3)
        if rand_position_1 == 1:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 + 1][rand_col_1] = 2
        if rand_position_1 == 2:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 - 1][rand_col_1] = 2
        if rand_position_1 == 3:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 + 1] = 2
    elif rand_col_1 in range(1,10) and rand_row_1 == 11:
        #can be 2, 3 or 4
        rand_position_1 = randint(1,3)
        if rand_position_1 == 2: #position 2
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 - 1][rand_col_1] = 2
        if rand_position_1 == 3: #position 3
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 + 1] = 2
        if rand_position_1 == 4: #position 4
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 - 1] = 2

After applying my code I get this error.

应用我的代码后,我收到此错误。

    Traceback (most recent call last):
  File "<stdin>", line 310, in <module>
  File "<stdin>", line 15, in PrintBoards
TypeError: sequence item 0: expected string, NoneType found

and here is the PrintBoards function

这是 PrintBoards 功能

def PrintBoards(Player,Opponent):
    print ' '*10, 'PLAYER', ' '*30, 'OPPONENT'
    letters = ['A','B','C','D','E','F','G','H','I','J','K','L']
    for x in range(6):
        print letters[x],"  ".join(map(DisplayChar,Player[x]))," "*18,"| ","  ".join(map(DisplayChar,Opponent[x]))
    for x in range(6,12):
        print letters[x],"  ".join(map(DisplayChar,Player[x]))," | ","  ".join(map(DisplayChar,Opponent[x]))
    print " ","  ".join(map(str,range(1,10)))," 10 11 12","  ","  ".join(map(str,range(1,10)))," 10 11 12"

and here is the DisplayChar function

这是 DisplayChar 函数

def DisplayChar(x):
    if x==0: 
        return '?'
    elif x==1:
        return ' '
    elif x==2:
        return 'X'
    elif x==3:
        return ' '
    elif x==4:
        return '*'

I tried editing the above function to this...

我尝试将上述功能编辑为此...

def DisplayChar(x):
        if x==0: 
            return '?'
        elif x==2:
            return 'X'
        elif x==4:
            return '*'
        else:
            return ' '

However it gave me this error instead

但是它给了我这个错误

Traceback (most recent call last):
  File "<stdin>", line 309, in <module>
  File "<stdin>", line 15, in PrintBoards
TypeError: argument 2 to map() must support iteration

I also tried printing the lists Player and Opponent after the PrintBoards function to ensure that they contain 0s and 1s (referring to the DisplayChar function) which they do (when inserted intot he original, not when I put my new and very long code in)

我还尝试在 PrintBoards 函数之后打印 Player 和 Opponent 列表,以确保它们包含它们所做的 0 和 1(指的是 DisplayChar 函数)(插入原始代码时,而不是我放入新的很长的代码时)

This next bit is in response to Michael

下一位是对迈克尔的回应

PLAYER                                OPPONENT
[[1, 1, 1, 1, 1, 1], [1, 2, 2, 2, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1], [1, 2, 2, 2, 1, 2, 2, 2, 2, 1, 1, 1], [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
[0, 0, 0, 0, 0, 0]
A                                                       |  ?  ?  ?  ?  ?  ?
<function Deploy_Destroyer_1 at 0x1c2634>
[0, 0, 0, 0, 0, 0]
B
Traceback (most recent call last):
  File "<stdin>", line 314, in <module>
  File "<stdin>", line 17, in PrintBoards
TypeError: argument 2 to map() must support iteration

EDIT

编辑

After someone kindly pointed out I had assigned the function instead of called it, I've found that another error occurred (I don't think Python likes me)

在有人善意地指出我分配了该函数而不是调用它之后,我发现发生了另一个错误(我认为 Python 不喜欢我)

Traceback (most recent call last):
  File "<stdin>", line 313, in <module>
  File "<stdin>", line 17, in PrintBoards
TypeError: argument 2 to map() must support iteration

Below I've also included where I called the function in case I've done something silly

下面我还包括了我调用函数的地方,以防我做了一些愚蠢的事情

Player, Opponent = InitBoards()
Player = DeployFleet(Player), Deploy_Destroyer_1(Player)
PrintBoards(Player,Opponent)

EDIT 2

编辑 2

I changed it to what Micheal0x2a said and it ran with no errors however the ship that the code is placing disappeared :s

我把它改成 Micheal0x2a 所说的,它运行没有错误,但是代码放置的船消失了:s

From what I understand, the PrintBoardsfunction prints the board for the Playerby mapping the items in the list to the DisplayCharfunction (if 2 is an item in the list, it prints X etc). So my novice knowledge tells me that the Deploy_Destroyer_1function should be called in Player =in the Mainfunction (included above) to ensure that the item in the list is changed, therefore the character printed should change.

据我了解,该PrintBoards函数Player通过将列表中的项目映射到函数来打印板DisplayChar(如果 2 是列表中的一个项目,它会打印 X 等)。所以我的新手知识告诉我,Deploy_Destroyer_1应该在函数中调用Player =Main函数(包括在上面),以确保列表中的项目发生变化,因此打印的字符应该发生变化。

I'm guessing that there is something wrong with my new code (Deploy_Destroyer_1) which doesn't do this correctly (either doesn't change the item in the list therefore doesn't print the correct character, or something else which I can't think of).

我猜我的新代码 ( Deploy_Destroyer_1) 有问题,它没有正确执行此操作(要么不更改列表中的项目,因此不打印正确的字符,要么其他我不能考虑到)。

However there is also a big chance that I have confused myself :)

然而,我也有很大的机会让自己感到困惑:)

I have only been learning Python for a couple of weeks so if anyone needs more detail in order to help me please ask

我只学了几周 Python,所以如果有人需要更多细节来帮助我,请询问

回答by Michael0x2a

The problem is most likely somewhere within these 4 lines:

问题很可能出现在以下 4 行中:

for x in range(6):
    print letters[x],"  ".join(map(DisplayChar,Player[x]))," "*18,"| ","  ".join(map(DisplayChar,Opponent[x]))
for x in range(6,12):
    print letters[x],"  ".join(map(DisplayChar,Player[x]))," | ","  ".join(map(DisplayChar,Opponent[x]))

Within these lines, you've used a joinstatement multiple times. The joinstatement requires a list of strings in order to work. However, when you're mapping DisplayCharto Player[x], the DisplayCharfunction is returning the value Noneinstead of a string of some sort.

在这些行中,您join多次使用了一个语句。该join语句需要一个字符串列表才能工作。但是,当您映射DisplayChar到 时Player[x],该DisplayChar函数将返回值None而不是某种字符串。

If you look at the DisplayCharfunction, it handles values only from 0 to 4. The lists you use probably include additional numbers or characters. If xhappened to be something like 5, DisplayCharwill terminate and simply return the value None. Remember, functions return the value Noneby default.

如果您查看该DisplayChar函数,它只会处理 0 到 4 之间的值。您使用的列表可能包含其他数字或字符。如果x碰巧是这样的5DisplayChar将终止并简单地返回值None。请记住,函数None默认返回值。

You either need to handle these additional numbers within DisplayChar, or modify DisplayCharto contain an elsestatement to return an empty string, like so:

您要么需要在 中处理这些额外的数字DisplayChar,要么修改DisplayChar为包含一条else语句以返回一个空字符串,如下所示:

def DisplayChar(x):
    if x==0: 
        return '?'
    elif x==1:
        return ' '
    elif x==2:
        return 'X'
    elif x==3:
        return ' '
    elif x==4:
        return '*'
    else:
        return ' '

Edit:

编辑:

Ok, I think I might know what's going on, given the new edits.

好的,考虑到新的编辑,我想我可能知道发生了什么。

Notice when you were printing out Player[x], it printed <function Deploy_Destroyer_1 at 0x1c2634>the second time around?

请注意,当您打印出来时Player[x],它打印<function Deploy_Destroyer_1 at 0x1c2634>了第二次?

That means somewhere, buried deep inside your code, you've done something to the effect of Player[row] = Deploy_Destroyer_1(notice the missing parenthesis!). Instead of calling the function, you've assignedthe function.

这意味着在某个地方,深埋在您的代码中,您已经做了一些事情Player[row] = Deploy_Destroyer_1(注意缺少的括号!)。您已经分配了该函数,而不是调用该函数。

Hunting for, and adding the missing parenthesis should most likely solve the problem.

寻找并添加缺少的括号最有可能解决问题。

Edit 2:

编辑2:

I think your problem is with this line: Player = DeployFleet(Player), Deploy_Destroyer_1(Player)

我认为您的问题出在这一行: Player = DeployFleet(Player), Deploy_Destroyer_1(Player)

If you try doing a print Playerimmediately after, I think you'll most likely see a large list of numbers, followed by None.

如果您尝试在print Player之后立即执行 a ,我认为您很可能会看到一大串数字,然后是None.

This is because the DeployFleetfunction is returning the table (I think?) whereas the Deploy_Destroyer_1function returns nothing. Instead, it just mutates the Playertable.

这是因为该DeployFleet函数正在返回表(我认为?)而该Deploy_Destroyer_1函数不返回任何内容。相反,它只是改变了Player表格。

To fix this, try doing either this:

要解决此问题,请尝试执行以下任一操作:

Player = DeployFleet(Player)
Deploy_Destroyer_1(Player)

...or modify Deployer_Destroyer_1so that it returns Playerwhen it's finished, so you can do this:

...或进行修改Deployer_Destroyer_1,使其Player在完成后返回,因此您可以执行以下操作:

Player = DeployFleet(Player)
Deploy_Destroyer_1(Player)

回答by Johannes Charra

Your DisplayChar function does not have a default value. This would do no harm if you'd be handling all possible cases for x, but apparently you're not. Try

您的 DisplayChar 函数没有默认值。如果您要处理 的所有可能情况x,这不会有什么害处,但显然您不是。尝试

def DisplayChar(x):
    if x == 0: 
        return '?'
    elif x == 2:
        return 'X'
    elif x == 4:
        return '*'
    else:
        return ' '

but this will probably yield blank strings where you don't expect them.

但这可能会在您不期望它们的地方产生空白字符串。

Generally, I'd recommend going through a good Python tutorial first. All of your above code can be greatly simplified.

一般来说,我建议先阅读一个好的 Python 教程。您上面的所有代码都可以大大简化。

回答by Noel Evans

If you've arrived here because you were looking for the root cause of "TypeError: sequence item 0: expected string, NoneType found", it can come from doing something along these lines...

如果您来到这里是因为您正在寻找“ TypeError: sequence item 0: expected string, NoneType found”的根本原因,那么它可能来自于按照这些方式做的事情......

','.join([None])