+ 1
Creating a simple calculator
in the first line.. while True... here what has to be true in order to proceed or print the options. in a commonsense context i expect a condition which is true or false .. but no condition is specified... so got confused
5 ответов
+ 2
It Would be best if you typed the actual code
+ 2
what your saying is this
while(a) {
//code
}
+ 2
what it sounds like your saying is an if statement,
if(a) {
//code
}
+ 1
Are you describing a lesson material?
If you want it to me sense just think of 'while True' as 'while 1=1'
as a matter of fact, that's the default meaning of while True
+ 1
while, if, elif, etc are conditional statements that must be made True in order for it to be used. This might sound confusing, but it makes sense if you think about it this way:
Think of them as ON (True) and OFF (False) switches. Boolean's (as they are called) represent this at a basic level. 1 and 0 = ON and OFF = True and False. You can even try this in Python:
print(True == 1)
print(False == 0)
While True - think of it as flipping an ON switch. Everything under the while block will run forever, unless there's a break under it.
The same logic works with others like if or elif. You can even test this yourself. Type this into Python:
if not True:
print("activated")
print("done")
Basically, by using the 'not', we made the if statement False, therefore it's switched off - the print statement under it will never fire. Lets try something you're familiar with:
a = 0
while a < 10:
a += 1
I'm sure this makes more sense to you. the while loop will only work, so long as a is less than 10. Since a is less than 10, the while loop is switched ON - therefore it is True.
Makes more sense? Lets go further!
remember the first example? Lets make it more interesting:
a = False
if a is False:
print("Activated")
print("done")
Don't be confused. Look at the if statement carefully. It asks, if the variable a is False, print "activated". Since the variable a is False, the conditions were met for the if statement, therefore it switches ON - and the statement is True.
It can be confusing at first, but the more you play with it, the easier it's to understand.