Python 对于循环-.split和int
我写了两个功能,工作得很好,但我想改变我的部分代码,使其更有效,使用for循环,每8个图表“跳跃”。 当我运行Python 对于循环-.split和int,python,Python,我写了两个功能,工作得很好,但我想改变我的部分代码,使其更有效,使用for循环,每8个图表“跳跃”。 当我运行验证\u校验和时,我得到: AttributeError: 'int' object has no attribute 'split' 但是,当我用带有#的注释行运行它时,它工作得很好。 你知道如何在不改变代码其他部分的情况下修复它吗?(有更多的函数可以使用这些函数,这会导致混乱) 我的代码: def xor_bytes(byte1, byte2): byte1, byte2=
验证\u校验和
时,我得到:
AttributeError: 'int' object has no attribute 'split'
但是,当我用带有#
的注释行运行它时,它工作得很好。
你知道如何在不改变代码其他部分的情况下修复它吗?(有更多的函数可以使用这些函数,这会导致混乱)
我的代码:
def xor_bytes(byte1, byte2):
byte1, byte2=byte1.split(), byte2.split()
xor=""
a=0
for i in byte1:
for j in i:
t=int(byte2[0][a])^int(j)
xor+="".join(str(t))
a+=1
return xor
def verify_checksum(datagram):
datagram=list(datagram)
org_checksum=datagram[48:56]
org_checksum="".join(org_checksum)
x=48
for i in datagram[48:56]:
datagram[x]='0'
x+=1
datagram="".join(datagram)
res=xor_bytes(datagram[0:8], datagram[8:16])
for i in (16,88,8):
res=xor_bytes(res, i)
#res=xor_bytes(res,datagram[16:24])
#res=xor_bytes(res,datagram[24:32])
#res=xor_bytes(res,datagram[32:40])
#res=xor_bytes(res,datagram[40:48])
#res=xor_bytes(res,datagram[48:56])
#res=xor_bytes(res,datagram[56:64])
#res=xor_bytes(res,datagram[64:72])
#res=xor_bytes(res,datagram[72:80])
#res=xor_bytes(res,datagram[80:88])
if res==org_checksum:
return True
else:
return False
输入:
verify_checksum("1111000000001111000011111111000001010101101010101010111001110011001000000110101101101001")
输出:
True
您有一个
for
循环,如下所示:
for i in (16,88,8):
res=xor_bytes(res, i)
然后尝试在xor\u bytes
函数中调用.split
oni
(这是一个整数):
byte1, byte2=byte1.split(), byte2.split()
(i
作为byte2
传入)
我不太确定你想在这里完成什么,所以我不能帮你解决这个问题,但这就是为什么它会发生的原因
也许你想要的是:
bytes_range = range(16,89,8) #make 89 the upper bound so that 88 is included in the range
for start,end in zip(bytes_range[:-1],bytes_range[1:]):
res = xor_bytes(res,datagram[start:end])
您有一个
for
循环,如下所示:
for i in (16,88,8):
res=xor_bytes(res, i)
然后尝试在xor\u bytes
函数中调用.split
oni
(这是一个整数):
byte1, byte2=byte1.split(), byte2.split()
(i
作为byte2
传入)
我不太确定你想在这里完成什么,所以我不能帮你解决这个问题,但这就是为什么它会发生的原因
也许你想要的是:
bytes_range = range(16,89,8) #make 89 the upper bound so that 88 is included in the range
for start,end in zip(bytes_range[:-1],bytes_range[1:]):
res = xor_bytes(res,datagram[start:end])
您的循环看起来与注释行不同
for i in range(16,88,8):
res=xor_bytes(res, datagram[i:i + 8])
您的循环看起来与注释行不同
for i in range(16,88,8):
res=xor_bytes(res, datagram[i:i + 8])
在注释掉的行中,传递两个字符串作为参数 在循环中,传递一个字符串和一个int作为参数
错误发生在byte2.split()上,因为它是数据报某一部分中的int.Pass,而不是数字位置,您会没事的。在注释掉的行中,您将两个字符串作为参数传递 在循环中,传递一个字符串和一个int作为参数
错误出现在byte2.split()上,因为它是数据报的一部分中的int.Pass,而不是数字位置,您会没事的。您的修复比我的好一点+你的建议比我的好一点+1.