Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 For loop prints strane values?
#1
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.
Quote
#2
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
newbieAuggie2019 likes this post
Quote
#3
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.
newbieAuggie2019 likes this post
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  very weird unexpected behaviour, related to prints allenhe 1 105 Nov-07-2019, 10:39 PM
Last Post: Gribouillis
  Visual Studio Code does not print desired output but only prints "..." vincentolivers 11 403 Sep-09-2019, 12:58 PM
Last Post: vincentolivers
  Get all values of for loop with an index BollerwagenIng 2 308 Aug-09-2019, 07:58 AM
Last Post: BollerwagenIng
  change array column values without loop khalidreemy 2 268 May-05-2019, 09:05 AM
Last Post: DeaD_EyE
  can you understand why this code prints None? arcbal 2 422 Mar-13-2019, 02:57 AM
Last Post: arcbal
  randint stops changing values in a loop Naito 4 518 Jan-30-2019, 08:15 AM
Last Post: perfringo
  What for a file that prints nothing sylas 1 443 Sep-12-2018, 01:18 PM
Last Post: gruntfutuk
  msvcrt.getch prints twice inside loop ReallyBigTeeth 5 2,026 Sep-04-2018, 06:54 AM
Last Post: ReallyBigTeeth
  My code prints out my string 5 times and then just stops? Abstract_Otaku 0 445 Jun-13-2018, 07:11 PM
Last Post: Abstract_Otaku
  Need help for finding cumulative values in a loop Lightning1800 10 1,557 May-23-2018, 07:48 PM
Last Post: Lightning1800

Forum Jump:


Users browsing this thread: 1 Guest(s)