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

Posted

技术标签:

【中文标题】类型错误:序列项 0:预期字符串,找到无类型【英文标题】:TypeError: sequence item 0: expected string, NoneType found 【发布时间】:2013-09-22 00:54:20 【问题描述】:

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

# 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)

第二次尝试根据条件调整坐标(如果它适合板上以及可以放置哪个旋转)。

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

应用我的代码后出现此错误。

    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

这里是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"

这里是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 '*'

我试着把上面的函数编辑成这个...

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

但是它给了我这个错误

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

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

下一点是对迈克尔的回应

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

在有人好心指出我分配了函数而不是调用它之后,我发现发生了另一个错误(我认为 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

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

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

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

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

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

不过也有很大可能是我自己搞糊涂了

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

【问题讨论】:

您需要显示完整的回溯,以便我们可以看到错误出现的位置。 这有帮助吗?你可能需要整个程序?啊 你的问题绝对不是TypeError 有任何建议@septi? 我们不知道PrintBoards 函数,但我猜它里面有一个join 将所有单元格内容连接在一起。这不适用于无。 【参考方案1】:

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

','.join([None])

【讨论】:

谢谢,这正是我需要知道的。如果您的列表中有其他内容,但仍包含无,也会发生这种情况。 [None, 'foo', 'bar'] 现在我必须找到从数组中清除 'None's 的方法 这是一个很好的方法:>>> filter(None, ['a', None, 'c']) @NoelEvans: +1,但你需要指出,这不会从列表中过滤掉 None,还会过滤任何其他虚假值。 受@NoelEvans 启发,我发现这很有用:','.join(map(str,[None])) 您,先生,让我免于花费无数个小时绝望地查看 Google Cloud 日志,这些日志几乎是不言自明的。【参考方案2】:

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

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

但这可能会在您不希望出现的地方产生空白字符串。

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

【讨论】:

将其更改为...它确实打印了另一行板,但出现此错误:S Traceback (most recent call last): File "&lt;stdin&gt;", line 308, in &lt;module&gt; File "&lt;stdin&gt;", line 15, in PrintBoards TypeError: argument 2 to map() must support iteration 对。您的意思可能是 DisplayChar(Player[x]) 而不是 map(DisplayChar,Player[x])【参考方案3】:

问题很可能在这 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]))

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

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

您要么需要在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 ' '

编辑:

好的,鉴于新的编辑,我想我可能知道发生了什么。

注意到当您打印出Player[x] 时,它第二次打印出&lt;function Deploy_Destroyer_1 at 0x1c2634&gt;

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

寻找并添加缺少的括号应该很可能解决问题。

编辑 2:

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

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

这是因为 DeployFleet 函数正在返回表(我认为?),而 Deploy_Destroyer_1 函数什么也不返回。相反,它只是改变了Player 表。

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

Player = DeployFleet(Player)
Deploy_Destroyer_1(Player)

...或修改Deployer_Destroyer_1,使其在完成后返回Player,这样您就可以这样做:

Player = DeployFleet(Player)
Deploy_Destroyer_1(Player)

【讨论】:

Player 是另一个函数def InitBoards(): #The following convention is used for storing the state of a square: #0=Unknown #1= Empty #2= Occupied #3= Missed #4= Hit (player or opponent) # Initially, the player's board is all empty, the opponent's is all unknown Player = [[1]*(6 if x&lt;6 else 12) for x in range(12)] Opponent = [[0]*(6 if x&lt;6 else 12) for x in range(12)] return Player,Opponent 中的定义列表,这是我的想法:map() 检查正方形/单元格的状态并使用DisplayChar 函数打印正确的字符串。我尝试更改显示 @BLeeM - 就在for 循环之后,您可以尝试编写print Player[x], Opponent[x] 以便我们可以检查这些列表中实际包含的内容吗?现在,我们只是在猜测这些值中的内容,我想确定一下。 它在InitBoards函数中定义(其中1和0指的是DisplayChardef InitBoards(): # Initially, the player's board is all empty, the opponent's is all unknown Player = [[1]*(6 if x&lt;6 else 12) for x in range(12)] Opponent = [[0]*(6 if x&lt;6 else 12) for x in range(12)] return Player,Opponent所以它只打印所有的1和0,因为我们对对手一无所知我还没有部署我的舰队 @BleeM -- 不管怎样,你还能打印出来吗?某些东西可能会以意想不到的方式修改它们。此外,在调试时,偏执并测试您的假设总是好的——如果您的假设是正确的,就不会有错误:) 它没有到达print 它像以前一样停止并给我traceback 错误:( 并感谢您的建议!【参考方案4】:

如果出现任何此类错误,请使用它: 例如 ','.join((map(str,video_url)) 地图功能帮助您将 tuble 更改为 str

【讨论】:

以上是关于类型错误:序列项 0:预期字符串,找到无类型的主要内容,如果未能解决你的问题,请参考以下文章

Python - 读取 .b4u 文件 - 错误序列项 0:预期的 str 实例,找到的字节

将 HTML 转换为 CSV

函数“矩形”中的(-5:错误参数) - 无法解析“pt1”。索引为 0 的序列项类型错误

SQL 错误:ORA-00932:不一致的数据类型:预期的 CHAR 得到了 NUMBER

错误:类型不匹配:推断类型是字符串?但布尔值是预期的

在 BigQuery 中加载 avro 文件 - 默认值的类型为意外类型。预期为 null,但找到字符串:“null”