0

I don't get the "while True: print("i dont get it")" please explain

there are times in the quiz here, i could see "while True: print(smthng)" or even "while False: print(smthng)". what is the value of True and False there? There were no prior assignment statement in the code. please help

9th Nov 2016, 4:13 PM
La Raine Salazar
La Raine Salazar - avatar
3 Respostas
+ 2
We use While True: when we don't know exactly how many times loop is going to iterate. When we have condition something like this while True: or while False: it usually have if condition and break statement in it. See below example While True: number = input ("Guess the Number") If (input == "5"): break else: print (number) Above program continues to run untill user enter 5. When if condition becomes true with the help of break statement it will jump out of the while loop.
9th Nov 2016, 8:22 PM
Waqas Asghar Bhalli
Waqas Asghar Bhalli - avatar
+ 1
You should review this answer, which explains their initial values, where they come from, that they can have values reassigned...etc. http://stackoverflow.com/a/2764099/3981745 Also note that it can help to think of True as 'something there' (like a number other than 0) and False as 'truly nothing' (just 0). For example, this works like True: while "string that isn't empty": loops forever : there's something there and this conditional like False: while "": never executes : nothing there
9th Nov 2016, 4:50 PM
Kirk Schafer
Kirk Schafer - avatar
0
To better understand this, lets replace True with a variable that takes a boolean value so that the statement reads while (var): print("blah"). If your variable was true, then the while loop will go until something changes your variable to false. Just explicitly stating True after the while statement is just that, it's just not being passed through a variable. That means while True: will always run the loop. Likewise, while False: will never run the loop.
9th Nov 2016, 4:19 PM
Lux
Lux - avatar