New course! Every coder should learn Generative AI!
Try a free lesson+ 1
How to stop a while loop. A quiz from SoloLearn
I recreated this code from a quiz, but in the Code Playground I get a complete different answer then what the "correct answer" was. Why does the n//i not change and stop the loop? https://code.sololearn.com/cFOcZ4h871D4
7 Answers
+ 3
tebkanlo idk why it doesn't work, but if you replace "while cond == 1:" woth "while n//i == 1:" it works fine
+ 4
And it is a infinite loop because you are not changing the value of cond so it remains an infinite loop as cond==1 true.
+ 4
Not sure what the code is trying to achieve, but here is an example of stopping a while loop through changing the condition as per Zatch bell suggestion.
n, i, cont = 10, 10, 0
cond = (n//i) # I simplified it with this variable
while ( (cond !=5) ):
i=i-1
cond +=1
cont = cont+1
print(f"after step {cont}\n i = {i}\n condition is {cond}")
+ 3
You can use break statement to stop a loop.
+ 2
๐ฅEACY๐ฅ that's odd, Idk why the other day didn't work.. even if it was written as you say and how was in the quiz (without the variable cond) ๐ค๐
Thanks you all๐
I'll try update my sololearn I guess
+ 1
Zatch bell I wanted to know how the quiz should have been written in order to work as the creator expected
Why is a break needed when (as the comment out print can show) the i changes?
The output is still a remainder 1, even when i=0 , shouldn't the condition change too with the iterations?
BTW at the beginning it was written as
"while n//i == 1:"
0
Use word break