Python Forum

Full Version: For loop prints strane values?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello. I have this code for printing all the values of "header" (a list of bytes) and the first value of "data_bytes" (another list of bytes):

def test (self):
		print ("Header: ")
		for x in self.header:
			print (x)
		print ("data_bytes: ")
		print (self.data_bytes [0])
The code that provides value to these both lists are:

self.data_bytes = rom_bytes [HEADER_SIZE:HEADER_SIZE + (16 + KB_SIZE * self.num_prg_blocks)]
		self.header = rom_bytes [0:HEADER_SIZE]
where HEADERR_SIZE is equal to 16.

So I expect header to go from 0 to 16, or seventeen values in total
I also expect data_byes go from 16 to a very large number that is irrelevant

Anyway, from what I see, the last value of "header" should be equal to the first value of "data_bytes"

However, the above "print ()" outcome it is:

Quote:78
69
83
26
2
1
1
0
0
0
0
0
0
0
0
0
data_bytes:
120

So I counted only sixteen values in "header", not seventeen. Plus the last value of "header" is different from the first of "data_bytes"

Also in "Bless" (a hexadecimal editor) the 16° value it is "120" with is the first printed value of "data_bytes)" But I expected "data_bytes" to start from number 16 counting from zero, or 17 in total, with shall be another value. This "120" should be the number before the last of "header".

So what I expect to be printed it is:


Quote:Header:
78
69
83
26
2
1
1
0
0
0
0
0
0
0
0
120
30936

and the first value of "data_bytes" to also be 30396, since it starts, for now, from the same byte that header ends. What I am misunderstanding?

Thanks for the help.
a better way to print this is:
header = ['one', 'two', 'three']
data_bytes = [1,2,3,4,5]
print(f"{[x for x in header]}, {data_bytes[0]}")
Output:
['one', 'two', 'three'], 1
You are misunderstanding indexes. Think of the indexes as being between the items, rather than on the items:

Output:
lunch = ['Spam', 'spam', 'spam', 'eggs', 'spam'] ^ ^ ^ ^ ^ ^ | | | | | | 0 1 2 3 4 5 -5 -4 -3 -2 -1
If you just give one index, you get the item after it. But if you give two indexes, you get everything between them. So lunch[0:5] is five items, not six.