Python ValueError:“X”不在列表中,为什么不能使用字符串查找和替换

Python ValueError:“X”不在列表中,为什么不能使用字符串查找和替换,python,python-3.x,list,Python,Python 3.x,List,因此,我有一个列表,列表中的数字是01-64之间的字符串,创建方式如下: gBoard = [ ['01','02','03','04','05','06','07','08'], ['09','10','11','12','13','14','15','16'], ['17','18','19','20','21','22','23','24'], ['25','26','27','28','29','30','31','32'], ['33','34

因此,我有一个列表,列表中的数字是01-64之间的字符串,创建方式如下:

 gBoard = [
    ['01','02','03','04','05','06','07','08'],
    ['09','10','11','12','13','14','15','16'],
    ['17','18','19','20','21','22','23','24'],
    ['25','26','27','28','29','30','31','32'],
    ['33','34','35','36','37','38','39','40'],
    ['41','42','43','44','45','46','47','48'],
    ['49','50','51','52','53','54','55','56'],
    ['57','58','59','60','61','62','63','64']]
在我的游戏中,我需要在这些数字中添加标记,因此如果玩家选择了该数字,它将如下所示:

gBoard = [
['01','02','03','04','05','06','07','08'],
['09','10','11 ##','12','13','14','15','16'],
['17','18','19','20','21','22','23','24'],
['25','26','27','28','29','30','31','32'],
['33','34','35','36','37 @@','38','39','40'],
['41','42','43','44','45','46','47','48'],
['49','50','51','52','53','54','55','56 ##'],
['57','58','59','60','61','62','63','64 ##']]
然后,从理论上讲,这会获取玩家的输入,找到该数字的第一次迭代,并将其索引保存到indexOne:

print(playerone, "It is your turn to play, type a number to replace with your token:\n")
playeronechoice = input("Type a number to replace with your token on the board:\n")
indexOne = gBoard.index(playeronechoice)
print ("##DEBUG:", indexOne)
gBoard[indexOne] = (playeronechoice, "##")
for row in gBoard: ##Shows the updated board
    print(row)
相反,它会像这样出错:

Type a number to replace with your token on the board:
60
Traceback (most recent call last):
  File "D:\####.py", line 101, in <module>
    indexOne = gBoard.index(playeronechoice)
ValueError: '60' is not in list

第101行:indexOne=gBoard.indexplayeronechoice

也许您可以使用Python的divmod直接计算索引,但首先减去1:

playeronechoice = int(input("Type a number to replace with your token on the board:\n"))

if not 1 <= playeronechoice <= 64:
    print("no such position exists")
else:
    row, col = divmod(playeronechoice - 1, 8)
    gBoard[row][col] = "@"                     # putting "@" as an example
当用户给出60,divmod59,8给出7,3。事实上,如果我们看第7行第3列第60列就在那里。我们在divmod中减去1,因为Python是0索引的

playerOneChoice = str(input("Type a number to replace with your token on the board:\n"))

for row in range(len(gBoard)):
    for col in range(len(gBoard[row])):
        if playerOneChoice == gBoard[int(row)][int(col)]:
            gBoard[int(row)][int(col)] = "$$"  # or concatenate with existing value
for row in gBoard:
    print(row)
输出


gBoard是一个列表列表。它的所有顶级元素都是列表,而不是字符串。您需要在子列表中搜索字符串。如果我不知道用户输入的内容会出现在什么列表中,我该怎么做?@E.V.A.你可以搜索所有的内容。对于每个子列表,请查看字符串是否存在。@E.V.A.您可以搜索每个列表,或创建索引到值的映射,以检查是否存在数字
Type a number to replace with your token on the board: 26
['01', '02', '03', '04', '05', '06', '07', '08']
['09', '10', '11', '12', '13', '14', '15', '16']
['17', '18', '19', '20', '21', '22', '23', '24']
['25', '$$', '27', '28', '29', '30', '31', '32']
['33', '34', '35', '36', '37', '38', '39', '40']
['41', '42', '43', '44', '45', '46', '47', '48']
['49', '50', '51', '52', '53', '54', '55', '56']
['57', '58', '59', '60', '61', '62', '63', '64']