Home python Who can explain the meaning of the Whele True cycle in Python?

Who can explain the meaning of the Whele True cycle in Python?




How can I understand what the While True is: …? I just realized that the cycle would be infinite until I register the Break team, but what is the meaning of the expression of the While True ? And is there a way out of such a cycle without break, for example, if “something” will be false : d

Maybe the question is stupid, but thanks to someone who will answer: D

Answer 1, Authority 100%

Typical situation To use this cycle, this is when the condition of completion becomes known only in the middle of iteration.

For example, our program should read from the console of the number, and calculate their product, until it is entered 0

total = 1
While True:
  x = int (input ())
  if x == 0:
  Total * = x
PRINT (Total)

Of course you can make the first input of the cycle, check the completion to transfer to the cycle header.

total = 1
x = int (input ())
While x! = 0:
  Total * = x
  x = int (input ())
PRINT (Total)

But in this case, the program ceases to satisfy the so-called. The principle of DRY .

In Python 3.8 there is a new design, which will allow you to get rid of While True in such cases

total = 1
While (x: = int (input ()))! = 0:
  Total * = x
PRINT (Total)

But the ideal solution is also not calling, and it will only be in every year or two, when most systems will use 3.8.

Also, do not forget that some cycles may certainly be endless, and in such cases without WHILE TRUE do not do

Def Count (X):
  While True:
    Yield X.
    x + = 1
For z in count (1):
  For y in Range (1, Z + 1):
    For x in Range (1, y + 1):
      if x ** 2 + y ** 2 == z ** 2:
        Print (f '{x} ** 2 + {y} ** 2 == {z} ** 2')

Answer 2, Authority 50%

This means that the cycle is constantly true. That is, until the truth == truth to perform.
You can without a break, but then instead of True, you will need to use a Boolean variable that will be equal to the truth. For example:

a = true
While A:
  a = false

(answer from the phone, sranny)

Answer 3

Bridically answered a person on the link: it’s the same thing that say while (6 & gt; 5), which is essentially while True. Infinite cycle, for the truth is always true: D
There, every line of response is very interesting and understandable so I will attach his answer here to read later those who are interested:

Everything Inside The () of the While Statement Is Going to Be Evaluated As A BOOLEAN. Meaning IT Gets Converted Into Either True or False.

Consider in the Statement While (6 & gt; 5)

IT First Evaluates The Expression 6 & GT; 5 Which Is True So Is The Same As Saying While (True)

Anything That Is Not False, 0, An Emptry String “”, NULL, OR UNALUED IS LIKELY TO BE EVALUATED TO TRUE.

When first Started Programming i Used to Do Things Like If (Foo == True), I Didn’t Realise That Was Virtually The Same Thing AS If (Foo).


So to Answer You Question: While True Is True.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions