Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 索引器,但我没有';我不知道怎么了_Python - Fatal编程技术网

Python 索引器,但我没有';我不知道怎么了

Python 索引器,但我没有';我不知道怎么了,python,Python,我只需要列表中的第一个元素“对手”,因此我编码: opponent = [1, 1, 1, 1, 1, 1] 我用这个来计算我的其他列表中的“对手”的数量 opponent = int(opponent[0]) 它说我的提交在 “行。”对手=int(对手[0]) 为什么会这样?我该如何解决这个问题(您的对手列表称为对手,稍后在您的代码中您会: if wongames.count(opponent) == 2: ...blablabla 覆盖先前的对手列表,因此现在对手名称引用一个整数 下次

我只需要列表中的第一个元素“对手”,因此我编码:

opponent = [1, 1, 1, 1, 1, 1]
我用这个来计算我的其他列表中的“对手”的数量

opponent = int(opponent[0])
它说我的提交在 “行。”对手=int(对手[0])


为什么会这样?我该如何解决这个问题(

您的对手列表称为
对手
,稍后在您的代码中您会:

if wongames.count(opponent) == 2:
...blablabla
覆盖先前的
对手
列表,因此现在
对手
名称引用一个整数

下次再次执行相同操作时:

opponent = int(opponent[0])
您试图访问整数的
[0]
索引,这显然不起作用,因为整数不能被索引

解决方案:只需为这两个变量使用不同的名称。我将调用列表
而不是

opponent = int(opponent[0])

请注意,由于它们已经是列表中的整数,因此不需要
int()
。只需使用
int()
如果您想将它从一种类型转换为另一种类型,例如,如果它是一个字符串
'0'
而不是一个整数
0
我不确定那里发生了什么,但我怀疑
索引器
是因为您正在分配相同的
对手
变量从列表中选择元素
op组件

如果在第二次迭代中运行此代码段,则变量
对手
不再是列表,这可能是错误的原因。请尝试更改将值分配给新变量

opponents = [1, 1, 1, 1, 1]
opponent = opponents[0]
如果代码块第二次执行,则会发生错误。请尝试将代码更改为以下内容:

opponent = [1, 1, 1, 1, 1, 1]

## Code block
opponent = int(opponent[0]) # Now opponent is not a list, just a number
if wongames.count(opponent) == 2:
    # do something


希望这有帮助!:)

你的问题不清楚。你应该发布你的代码。你为什么要在这里对int进行转换?int(opponent[0])。将变量名称
opponent
更改为其他变量。您正在将
对手
列表重新定义为步骤
对手=int(对手[0])
中的一个整数。因此,您不能将其用作列表。
opponent = [1, 1, 1, 1, 1, 1]
first_opponent = int(opponent[0]) 
if wongames.count(first_opponent) == 2:
    # do something