簡體   English   中英

我該如何解決這個IndexError?

[英]How I solve this IndexError?

我的問題是我需要將此Java代碼更改為python:

public static byte x1=0,x2=0,x3=0,x4=0,x5=0,x6=0;

這是Java代碼中的python

賈蒙對我說,我只能將所有內容放在這一行中:

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

我已經看過其他文章,但還沒有了解,這是代碼的一部分:

# -*- 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;
        ...

明確地說,我的問題是: 如何在python中創建不超出范圍的字節數組?

感謝您為此提供的所有支持。

拜托,不要對我強硬,我只是在學習

為了解決這個問題,我做了一些更改

if(x[0]==1):
if((x[1]&1)==1):

if(x[0]==0):
if((x[1]&1)==0):

這樣我可以解決問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM