+ 3

for loops

consider the loop for a in range(10): # some code I cannot change the value of a while the loop is still running, e.g. for a in range(10): if a == 5: a += 1 is there any way of doing this without using a while loop instead (example below)? a = 0 while a < 10: if a == 5: a += 1 # doesn't generate an error

18th Dec 2017, 6:09 PM
🐙evil octopus
🐙evil octopus - avatar
3 Respuestas
+ 2
right... that would only allow for incrementing the value of a by 1 though. if I wanted a -= 1, that wouldn't work.
18th Dec 2017, 6:34 PM
🐙evil octopus
🐙evil octopus - avatar
+ 2
it's just less elegant, idk. but thanks anyways
18th Dec 2017, 8:55 PM
🐙evil octopus
🐙evil octopus - avatar
0
Instead of a+=1 or a++ use “continue” to skip to next index without doing anything . Found via Google.
18th Dec 2017, 6:23 PM
H Chiang