Java 我如何解决这个索引器?
我的问题是,我需要将此Java代码更改为python:Java 我如何解决这个索引器?,java,python,python-2.7,Java,Python,Python 2.7,我的问题是,我需要将此Java代码更改为python: public static byte x1=0,x2=0,x3=0,x4=0,x5=0,x6=0; 这是python的Java代码 jammon对我说,我只能把所有这些放在这行: x = [0]*6 但现在当我运行代码时,我看到: Traceback (most recent call last): File "test.py", line 62, in <module> # The line 62 is the loc
public static byte x1=0,x2=0,x3=0,x4=0,x5=0,x6=0;
这是python的Java代码
jammon对我说,我只能把所有这些放在这行:
x = [0]*6
但现在当我运行代码时,我看到:
Traceback (most recent call last):
File "test.py", line 62, in <module> # The line 62 is the location of x[0]=datosOEM[k];
x[0]=datosOEM[k];
IndexError:bytearray index out of range
回溯(最近一次呼叫最后一次):
文件“test.py”,第62行,在#中,第62行是x[0]=datosOEM[k]的位置;
x[0]=datosOEM[k];
索引器:bytearray索引超出范围
我已经看了其他帖子,但我还没有看到。这是代码的一部分:
# -*- coding: utf-8 -*-
import serial
datosOEM=bytearray(5)
print(datosOEM)
datosOEM[0]=65
print(datosOEM)
x=[0]*6
k=0;
trama=1;
B=0;
C=0;
conexion=True;
resp=0
if(conexion):
#{
print ('conexion ON')
while(resp<200):
print ('data save')
while(C==0):
#{
print ('what is C?')
x[0]=datosOEM[k];
if(x[0]==1):
#{
print ('what is x[0]?')
x[1]=datosOEM[k+1];
if((x[1]&1)==1):
#{
print ('what is x[1]?')
C=1;
#}
#}
k=k+1;
#}
k=k-1;
...
#-*-编码:utf-8-*-
导入序列号
datosOEM=bytearray(5)
打印(datosOEM)
datosOEM[0]=65
打印(datosOEM)
x=[0]*6
k=0;
trama=1;
B=0;
C=0;
conexion=True;
resp=0
如果(conexion):
#{
打印('conexion ON')
虽然(resp我做了些什么来解决这个问题,但我还是改变了if
if(x[0]==1):
if((x[1]&1)==1):
到
通过这种方式,我可以解决这个问题。您的[0]*6
应该可以工作。如果不行,请尝试:[0对于范围(6)中的I)]
问题可能是datosOEM[k]
而不是x[0]
[0对于范围(6)中的I]也不工作。代码是公共静态字节[]datosOEM=new byte[900000];然后我按照Craig和user3286261的说明进行更改。你能展示整个代码吗?问题在于你没有展示的部分。作为一个好建议:Python与java不同:-条件不需要大括号,命令也不以分号结尾。
if(x[0]==0):
if((x[1]&1)==0):