Reputation:
I'm new to coding and there is a simple problem I do not understand how to fix. I'm just playing around with what I currently learned in Python. The loop in the second code "print("Breaking!") and break does not break the while loop i+=1. How can I fix this?
i=0
while True:
i+=1
if i==10000:
print("SKIP 10000")
continue
if i==10025:
print("FINISH")
break
print(i)
b="cyka\n"
a=int(input("#"))
if a>=10000:
print(b*a)
elif a<=10000:
while True:
i+=1
if i==10000:
print("Breaking!")
break
print(i)
Upvotes: 0
Views: 103
Reputation: 733
Since indeed there is no reset, and the value is the same as previous break so it will create infinite loop if you do it this way:
i=0
while True:
i+=1
if i==10000:
print("SKIP 10000")
continue
elif i==10025:
print("FINISH")
break
print(i)
b="cyka\n"
a=int(input("#"))
if a>=10000:
print(b*a)
elif a<=10000:
while True:
i+=1
if i==10000: # you need to change this value if you dont want infinite loop.
print("Breaking!")
break
I reviewed your code again since you can fix a while true loop always if you do it the right way without resetting. This should give you the expected result :
i=0
while True:
i+=1
if i==10000:
print("SKIP 10000")
continue
elif i==10025:
print("FINISH")
break
print(i)
while True: # put while true here will fix your problem without reset.
i=10000
b="cyka\n"
a=int(input("#"))
if a>=10000:
print(b*a)
elif a<=10000:
#while True: delete this line
i+=1
if i==10000:
print("Breaking!")
break
print(i)
Upvotes: 0
Reputation: 143
Not sure what you want to do in this program but I think you made the following mistakes:-
Solution as follows:-
i=0
while True:
i+=1
if i==10000:
print("SKIP 10000")
continue
if i==10025:
print("FINISH")
break
print(i)
b="cyka\n"
a=int(input("#"))
i=0 #reset i here
if a>=10000:
print(b*a)
elif a<=10000:
while True:
i+=1
#if i>=10000: <-- more stable alternative
if i==10000:
print("Breaking!")
break
print(i)
Upvotes: 2