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
TypeError: sequence item 0: expected string, NoneType found
提问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 PrintBoards
function prints the board for the Player
by mapping the items in the list to the DisplayChar
function (if 2 is an item in the list, it prints X etc). So my novice knowledge tells me that the Deploy_Destroyer_1
function should be called in Player =
in the Main
function (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 join
statement multiple times. The join
statement requires a list of strings in order to work. However, when you're mapping DisplayChar
to Player[x]
, the DisplayChar
function is returning the value None
instead of a string of some sort.
在这些行中,您join
多次使用了一个语句。该join
语句需要一个字符串列表才能工作。但是,当您映射DisplayChar
到 时Player[x]
,该DisplayChar
函数将返回值None
而不是某种字符串。
If you look at the DisplayChar
function, it handles values only from 0 to 4. The lists you use probably include additional numbers or characters. If x
happened to be something like 5
, DisplayChar
will terminate and simply return the value None
. Remember, functions return the value None
by default.
如果您查看该DisplayChar
函数,它只会处理 0 到 4 之间的值。您使用的列表可能包含其他数字或字符。如果x
碰巧是这样的5
,DisplayChar
将终止并简单地返回值None
。请记住,函数None
默认返回值。
You either need to handle these additional numbers within DisplayChar
, or modify DisplayChar
to contain an else
statement 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 Player
immediately after, I think you'll most likely see a large list of numbers, followed by None
.
如果您尝试在print Player
之后立即执行 a ,我认为您很可能会看到一大串数字,然后是None
.
This is because the DeployFleet
function is returning the table (I think?) whereas the Deploy_Destroyer_1
function returns nothing. Instead, it just mutates the Player
table.
这是因为该DeployFleet
函数正在返回表(我认为?)而该Deploy_Destroyer_1
函数不返回任何内容。相反,它只是改变了Player
表格。
To fix this, try doing either this:
要解决此问题,请尝试执行以下任一操作:
Player = DeployFleet(Player)
Deploy_Destroyer_1(Player)
...or modify Deployer_Destroyer_1
so that it returns Player
when 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])