+ 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 Answers
+ 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!