我正在努力改进战舰游戏。原始版本工作正常,没有错误。我已经编写了代码来帮助克服第一个版本每次都将船放在同一个地方的事实,所以我开始使用一艘船(由两个方格组成)。我通过创建两个函数来完成此任务:第一个生成随机坐标...
# 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函数)(当插入原始文件时,而不是当我放入新的和非常长的时候)代码)
下一部分是对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
修改
在有人指出我分配了这个功能而不是调用它之后,我发现发生了另一个错误(我不认为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)
编辑2
我将它更改为Micheal0x2a所说的并且它没有错误地运行但是代码所放置的船只消失了:s
根据我的理解,PrintBoards
函数通过将列表中的项映射到Player
函数来打印DisplayChar
的电路板(如果2是列表中的项,它打印X等)。所以我的新手知识告诉我Deploy_Destroyer_1
函数应该在Player =
函数中调用Main
函数(包括在上面),以确保列表中的项目被更改,因此打印的字符应该改变。
我猜我的新代码(Deploy_Destroyer_1
)有问题但是没有正确执行此操作(要么不更改列表中的项目,要么不打印正确的字符,或其他我无法想到的东西)。
然而,我也很有可能让自己感到困惑:)
我只学习了几个星期的Python,所以如果有人需要更多细节以帮助我,请询问
答案 0 :(得分:77)
如果你是因为寻找“TypeError: sequence item 0: expected string, NoneType found
”的根本原因而来到这里的,那可能来自于这些方面的做法......
','.join([None])
答案 1 :(得分:1)
您的DisplayChar功能没有默认值。如果你要为x
处理所有可能的案件,这不会有害,但显然你不是。尝试
def DisplayChar(x):
if x == 0:
return '?'
elif x == 2:
return 'X'
elif x == 4:
return '*'
else:
return ' '
但是这可能会产生空白的字符串,而你不期望它们。
通常,我建议首先阅读一篇优秀的Python教程。您可以大大简化上述所有代码。
答案 2 :(得分:0)
问题很可能出现在这四行中:
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
恰好是5
,DisplayChar
将终止,只返回值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]
时,它第二次打印<function Deploy_Destroyer_1 at 0x1c2634>
?
这意味着隐藏在代码深处的某个地方,你已经完成了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)
答案 3 :(得分:0)
如果出现任何此类错误,请使用它: 例如 ','.join((map(str,video_url)) map 函数帮助您将 tuble 更改为 str