+ 1
Understanding how for i in s and how for i in range(len(s)) work.
For the security code coach problem while the input is "xxxGGxx$xxGxxT" The loop in this code: https://code.sololearn.com/cIwhQ9c8d1MA/?ref=app Works But the loop in this code: https://code.sololearn.com/cmE85mXaDWbe/?ref=app Does not work In my understanding they both should work in similar fashion, but the 2nd code's loop only takes the first G(intend 3) and not the other Gs. Why does this happen?
2 odpowiedzi
+ 2
Because you’re “working” code compares the index using “i” which is correct. This refers to the number 11 which is the position of the third ‘G’.
In your “faulty” code, your index is called by ‘G’ which regardless of how often you loop around, it will always call the first ‘G’ because index() returns the first instance.
s.index(‘G’) returns s[4] because the first ‘G’ happens at index 4.
+ 1
That makes sense, thanks!