[SOLVED] For loop on an array of strings that is returning the index on first loop, then the string on subsequent loops?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Richardv07

So on one of my scenes, you can choose the order that different colored orbs have to be deposited.

Like so:
https://i.imgur.com/58mRrnv.png

If the player doesn’t deposit the orbs in the correct order, it doesn’t count towards the score.

At a different place, I am using a for loop like the one below:

for i in range(0, orbcolororder.size()):
	print (orbcolororder[i])
	print ("printed i")

I added the “printed i” line to make sure that there wasn’t a random number being output somewhere, because when I run this loop, for the first value of the array, it prints 3, which is the position of “Blue” in the set of strings you can choose from in the dropdown above.

For the second value (and subsequent ones) of the array, it prints the string “Blue,” or whatever the string value that the second position in the array is instead of the index.

I don’t really care which way it prints, I can use either for what I’m doing, I just need it to be consistent.

Any help would be greatly appreciated.

I don’t quite follow. What’s your question, exactly? Do you want the colors, along with their index number, to be printed?

Ertain | 2019-05-27 23:58

I’m trying to access the color, either its string or its index. When I run the for loop, the first loop returns the first color, via its index, but then the second loop returns the second color via its string.

Trying to figure out why the first iteration of the loop returns an index number and all subsequent loops return a string.

The actual practical use of the loop is to tally the colors selected to define the color of a sprite, but the main issue here is that my for loop is returning a different type on the first loop than in subsequent loops, which is throwing an error.

Richardv07 | 2019-05-28 00:09

From what I gather (at least from your screenshot), the array orbcolororder has a size of 2, meaning that the array may look like this:

orbcolororder = [ 0, "Red, Yellow, Green, Blue, Purple" ]

The first element could have the value of 0, while the rest of it looks like the string "Red, Yellow, Green, Blue, Purple". Of course, I could be misreading, and be completely off.

Ertain | 2019-05-28 03:23

I don’t believe that to be the case, but I appreciate the input.

Edit: For example, if I print my array, it’s

[3, Red]

Which is Blue, Red, which I have the array set to right now. Just for whatever reason that first value is returning the index of the color instead of the string at that index.

The whole thing is weird, but I found a working solution.

I had to specify String() or int(), for the value I was grabbing from the first slot of the array, otherwise it wouldn’t work in the situation I was using it in.

int(orbcolororder[i])

and

String(orbcolororder[i])

Both work correctly and return the correct value, so who knows.

Richardv07 | 2019-05-28 03:41

:bust_in_silhouette: Reply From: Richardv07

I had to specify String() or int(), for the value I was grabbing from the first slot of the array, otherwise it wouldn’t work in the situation I was using it in.

int(orbcolororder[i])

and

String(orbcolororder[i])

Both work correctly and return the correct value, so who knows.