li = []
n = int(input("Enter numbers: "))
count = 0
for i in range(0,n):
a=int(input())
li.append(a)
print( li)
s = int(input('\nEnter a number for search : '))
for j in li:
if s == j:
li.remove(j)
count+=1
if count == 1:
print(f" {j} is removed . \n")
break
#else part is not executed
else:
print(f"Nothing is removed. \n")
print(li)
The else statement you identified is not executed because it lines up with the if count == 1: statement above it. Since count always equals 1 the first time through the loop and you break in the if statement, you exit the loop before count can be anything other than 1.
If you want the else statement to match the if s == j: statement then will have to outdent it. Note, however, once you do that you will have other problems based on your loop logic, but hey, one thing at a time. ;^)
It looks like the else statement belongs to the wrong if statement. Try moving the else statement back one indent. This will print Nothing is removed each time an element is not found.
for j in li:
if s == j:
li.remove(j)
count+=1
if count == 1:
print(f" {j} is removed . \n")
break
#else part is not executed
else:
print(f"Nothing is removed. \n")
print(li)
For the code below, after the first instance where the IF statement is false (i.e. for i=9), I understand that we go to the print("something") step however, my question is why does it infinitely print "something" and "somethingelse" instead of printing these two just once each then just ending? I mean we have no step after the print of these two that tells it to go back to the if statement and test again with i = 9 (that would lead to an infinite loop)..
i=1
while True:
if i%9 != 0:
print ("inside if")
i +=1
continue
print("something")
print("somethingelse")
Your print statements are inside of a While True: loop with no exit criteria. That is precisely why.
while True:
Means that while I do not have a statement somewhere to stop or a break to stop the algorithm, it will keep entering in the while loop.
Therefore, what you made is an infinite loop and that's why it keeps printing the same stuff.
i=1
while True:
if i%9 != 0:
print ("inside if")
i +=1
break
print("something")
print("somethingelse")
#output
"inside if" -> breaks after `i=1` because `1%9=1`
"something"
"somethingelse"
i=1
kak = True
while kak:
if i%9 != 0:
print ("inside if")
i +=1
kak = False # --> something to tell the algorithm, hey! stop!
print("something")
print("somethingelse")
Python newbie. I am trying to print number after taking input from user using while loop.
my code takes int from user and then runs the loop. after printing the first number the code asks to continue after which the loop will continue.
My code is below:
i = 1
ans = 'n'
x = int(input("enter a number to loop: " ))
while(i<x):
print('\n')
print(i, end= " ")
ans = input('\ndo you want to print the next number? ')
if ans == 'y':
i += 1
else:
print('thanks')
The code is doing and extra loop and then after getting the answer terminates the loop. On top of that on the last loop if the answer is 'n' it keeps running.
You just have to add a break statement in your code, right before the print("thanks") line. That will exit the loop.
else:
break
print('thanks')
All loops will continue unless told to break or become false.
Break and continue are very important in programming. I suggest you read this https://www.tutorialspoint.com/python/python_loop_control.htm
My problem - and I don't know why there is a keyword continue, which should leave the value of 3 and go further. In fact, I have a loop that is infinite - that is, it crashes the program.
tab = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
i = 0
while i < len(tab):
print(tab[i])
if tab[i] == 3:
continue
i+=1
It looks like you are trying to iterate through the list until you find a 3 then break. Do something like this:
items = [1,2,3,4,5]
for item in items:
if item == 3:
break
The keyword continue will pass to the next iteration of the loop, where break will stop the loop.
The continue keyword continues with the next iteration of the loop.
In your case, it prevents the statement i+=1 to be executed.
Here is what happens:
Loops through 0,1,2 just fine
When it evaluates tab[i] = 3 it proceeds with the next iteration of the loop and i+=1 is never executed, hence i remains 3 and never gets incremented. It keeps doing this forever.
If you want to exit the loop, you can use the break statement instead of continue.
For more information, you can read on the continue keyword here: https://docs.python.org/3/tutorial/controlflow.html
I think the answers here all assume that OP wanted to use break instead of continue.
However, if you need to use continue in a while loop, make sure you have an increment/decrement operation above the continue statement, because the continue statement jumps to the top of the while loop and, in your case, i remains 3.
Respectively, the continue statement in a do-while loop jumps to the bottom of the loop. So, in your case you have two options, either you increment above the continue or use a for-loop.
tab = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
i = -1
while i < len(tab):
i += 1
print(tab[i])
if tab[i] == 3:
continue
OR
for i in range(0,15):
if i == 3:
continue
print(i)
You're using the continue keyword, but I think you want break.
Your code is running forever because i never iterates past 3. Once i == 4, it enters your if statement and continues the loop. Since it continues, it never iterates after that.
Because you are using the continue keyword that skips the rest of the code.
Once i reaches 3, it skips over the i+=1 command. It appears that you want to use the keyword break. You could also do:
for i in tab:
print(i)
if i == 3:
break
If you are trying to skip the value "3" then
tab = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
i = 0
while i < len(tab)-1:
i+=1
if tab[i] == 3:
continue
//i+=1 => infinite loop
print(tab[i])
I have a problem in a simple code with while loop. My problem is explained in code comments.
CODE
exit = False
while not exit:
choice = input("Test ")
if choice== 1:
print "hello"
exit = False
else:
print "good morning"
#I want to return to the first while with the input Test but I pass to the second while
exit = False
exit1 = False
while not exit1:
choice = input("Test 1")
if choice== 1:
print "bye"
exit1 = False
else:
print "good evening"
#I want to return to the first while with the input Test but I return to the second while
exit = False
Thanks a lot.
I think what are you looking for are the continue and break statements.
The continue will interrupt the current iteration (and a new iteration is started, of course).
The break will interrupt the smallest enclosing loop.
Both statements work for for as well.
Take a look here for some reference.
outer = True
while outer:
do_something
if a:
continue # will get you back to do_something
if b:
break # will end the outer loop
If you set outer to False somewhere, it will end the while loop in the next iteration.