gpt4 book ai didi

python - TypeError:序列项 0:预期字符串,未找到 NoneType

转载 作者:太空狗 更新时间:2023-10-29 17:08:56 28 4
gpt4 key购买 nike

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

# 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 函数(如果 2 是列表中的一项,则打印 X 等)。所以我的新手知识告诉我,Deploy_Destroyer_1 函数应该在 Player = 中的 Main 函数(包含在上面)中调用,以确保项目列表中的内容已更改,因此打印的字符应该更改。

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

不过也有很大的可能是我把自己弄糊涂了

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

最佳答案

如果您来到这里是因为您正在寻找“TypeError:序列项 0:预期的字符串,未找到 NoneType”的根本原因,那么它可能来自于沿着这些方向做的事情。 .

','.join([None])

关于python - TypeError:序列项 0:预期字符串,未找到 NoneType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18852324/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com