String looping


For fun, in the string looping (5/13) ‘exercise’ I changed the print command in the 2nd part to:

word = "Programming is fun!"
for letter in word:

Getting at the index in the list

if letter == “i” or letter == “r”:
print word.index(letter)

which gives 1, 1, 8, 8
Does “letter” not increment with the list index value? And if not, then why does it give two different values (1 and 8) and not just one when all four instances fulfill the same conditional statement? Python, you have me stumped…


If you call word.index('i') 1000 times, would it be more reasonable that it always produced the same value each time, or that it started switching around for no apparent reason?
And, in particular, keep in mind that nothing will ever do what you want it to do because you want it or expect it, it’ll have some particular behaviour which you can leverage – therefore you should look up what things do, not guess.


OK, I see what you mean now, but maybe you could try and cut me some slack: I’m actually trying to learn Python, and therefore not a master. I’m bound to not get ‘all implications’ of python’s code all at once. I’m not guessing - I tried multiple things, looked at previous examples, searched internet - but at that point I honestly didn’t see the mistake in why it was not giving me 1, 4, 8, 12 for the indices.


I do need to make that point though - guessing that something works one way, especially repeatedly, will get it wrong, that the way to go about it is to look it up.

Sorry that it came out too rough


Also, I may have misread where the question

You were doing word.index('i') and word.index('r') right? so that’s two different letters, two different positions. They’re repeating because you did the same call multiple times

but then again, perhaps not, because that’s what you describe here:

You must have gotten that assumption from somewhere yeah? That thinking pattern needs to go. :>

But it’s clear now right? That because you sent the method identical information multiple times, the result was the same each time? And that it would be very strange if the results varied under identical circumstances? And that, in particular, what would be required is to send additional information somewhere, under the hood, … there’s generally no hidden communication anywhere or it would get very confusing very quickly. The for-loop doesn’t affect how functions/methods behave etc. It’s just things being carried out exactly as written.


Thanks. Appreciated!
Yes, and by asking the question plus its answer I actually learned more than the lessons teach (this printing request was not an exercise, remember): I learned that ‘letter’ in this for-loop (or any “counter” in any for-loop) is not a counter that is incremented and thereby walks through the list from start to end, plus I learned that searching a list with multiple identical entries needs more thought but also gives possibilities.


What for-loops do is to request the next value repeatedly until there are no more – this means that the value has to have code which can send those values, it has to be iterable.
(iterables can create another separate value which is used to do the looping, that value is what produces the values until it has been exhausted)

a = range(5)
iterator = iter(a) # obtain an iterator from a
next(iterator)  # 0
next(iterator)  # 1
next(iterator)  # 2
next(iterator)  # 3
next(iterator)  # 4
next(iterator)  # error, end of loop


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.