I want to "reset" my for loop so i thought i have to set my iterator i on 0, but it will continue with 1 etc. How do "reset" my for loop? I tried to set i on 0 but in the next step my for loop just continues with 1 and so on. g is just a list with 2 items and S is a String, ignore all that. Its just this iteration that wont work
for i in range(0, len(g)):
print("gi = " + g[i])
print(i)
if g[i] in S:
S = S.replace(g[i], "", 1)
print(S)
i = 0
output += 1
you can use a while loop:
S='1112'
g=['1', '2']
i = 0
output = 0
while i < len(g):
if g[i] in S:
S = S.replace(g[i], "", 1)
print(S)
i = 0
output += 1
else:
i += 1
print('Is S empty?', S=='')
output:
112
12
2
Is S empty? True
Don't use a for
loop; use a while
:
i = 0
while i < len(g):
<your original loop body; might need to replace i=0 with i=-1>
i += 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.