Game Development Reference
In-Depth Information
4 - Guess the Number
False
The condition 0 < 6 returns the Boolean value True because the number 0 is less than
the number 6 . But because 6 is not less than 0 , the condition 6 < 0 evaluates to False .
50 is not less than 10 , so 50 < 10 is False . 10 is less than 11 , so 10 < 11 is True .
But what about 10 < 10 ? Why does it evaluate to False? It is False because the
number 10 is not smaller than the number 10 . They are exactly the same size. If a girl
named Alice was the same height as a boy named Bob, you wouldn't say that Alice is taller
than Bob or that Alice is shorter than Bob. Both of those statements would be false.
Try entering some conditions into the shell to see how these comparison operators work:
>>> 10 == 10
True
>>> 10 == 11
False
>>> 11 == 10
False
>>> 10 != 10
False
>>> 10 != 11
True
>>> 'Hello' == 'Hello'
True
>>> 'Hello' == 'Good bye'
False
>>> 'Hello' == 'HELLO'
False
>>> 'Good bye' != 'Hello'
True
Notice the difference between the assignment operator ( = ) and the "equal to" comparison
operator ( == ). The equal ( = ) sign is used to assign a value to a variable, and the equal to
( == ) sign is used in expressions to see whether two values are equal. It's easy to
accidentally use one when you meant to use the other, so be careful of what you type in.
Two values that are different data types will always be not equal to each other. For
example, try entering the following into the interactive shell:
>>> 42 == 'Hello'
False
>>> 42 != 'Hello'
 
Search WWH ::




Custom Search