How to stop a while loop. A quiz from SoloLearn | Sololearn: Learn to code for FREE!
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

29th Dec 2020, 5:31 PM
tebkanlo
tebkanlo - avatar
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
29th Dec 2020, 7:04 PM
๐Ÿ”ฅEACY๐Ÿ”ฅ
๐Ÿ”ฅEACY๐Ÿ”ฅ - avatar
+ 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.
29th Dec 2020, 5:34 PM
The future is now thanks to science
The future is now thanks to science - avatar
+ 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}")
29th Dec 2020, 8:02 PM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
You can use break statement to stop a loop.
29th Dec 2020, 5:32 PM
The future is now thanks to science
The future is now thanks to science - avatar
+ 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
30th Dec 2020, 10:40 AM
tebkanlo
tebkanlo - avatar
+ 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:"
29th Dec 2020, 6:17 PM
tebkanlo
tebkanlo - avatar
0
Use word break
29th Dec 2020, 6:31 PM
Arman