+ 2
What is the use of infinite loop? Where does it use? What is "true" in given code? What is the use of it?
3 ответов
+ 6
An infinite loop is a block of code that, intentionally or not, will repeat the execution of the loop body infinitely (never stop). By right, every loop we create must have a final condition, that is a where the requirements to evaluate a condition as true are met, and by then, the loop must cease repetition.
Since you used while true: , your code may become an infinite loop, if it didn't have the condition checking branch, which you did using the following line:
if text == "":
break
The lines above checks whether if the line that's read from the file has any content, and it instructed the loop to stop when a blank line was found (final condition is met)
There is other condition set in the code, that checks if the line read from the file begins with # character, the branch than instructed that the loop to ignore the line, and continue to the next loop iteration:
if text[0] == '#':
continue
Conclusion, without the if text == "": your loop is an infinite loop, it will keep on repeating the instructions, "forever".
Hth, cmiiw
+ 4
You're welcome, glad to help : )
+ 3
thank you