Python Boolean Logic

Chris Warrick kwpolska at gmail.com
Sat Sep 23 04:41:12 EDT 2017


On 23 September 2017 at 06:46, Cai Gengyang <gengyangcai at gmail.com> wrote:
> Output :
>
> ('bool_one = ', False)
> ('bool_two = ', False)
> ('bool_three = ', False)
> ('bool_four = ', True)
> ('bool_five = ', False)

You’re using Python 2 with Python 3-style print statements. To make it
look good, start your code with:

from __future__ import print_function

Or use the Python 2 form (without parentheses), or even better: switch
to Python 3.

Now, Codecademy is a peculiar place. They still teach Python 2 (that
sucks!) and have a specific teaching style. The way you’re supposed to
solve this is to just assign your answers to bool_one through
bool_five. You should just type True or False in each blank, using
Python to evaluate this expression is kinda cheating. Codecademy will
then tell you if you got the right answer. Don’t print stuff you
aren’t asked to print.

However, you should not depend only on Codecademy’s interpreter.
Install Python on your computer and work with that. I also recommend
learning using different resources, and learning Python 3. It’s the
future.

~~~

On 23 September 2017 at 07:01, Bill <BILL_NOSPAM at whoknows.net> wrote:
> Your answers appear correct, but you could write Python statements to test
> them (or any you encounter in the future). For instance,
>
> if (20 - 10)  > 15 :
>     print("true")
> else:
>     print("false");
>
> Or,
>
> s='(20 - 10)  > 15'
> b=(20 - 10)  > 15
> print(s, " is ", ("true" if b else "false") );  ## inside parentheses may be
> removed.

This outputs "False is false", because you used the variable in your
expression. You can just do this:

>>> print("s is", s)

This will print "s is False".

You can also replace your earlier `if` with:

>>> print((20 - 10) > 15)

(False will appear upper-case, of course.)

PS. don’t use semicolons with Python. Avoid eval() as well.

-- 
Chris Warrick <https://chriswarrick.com/>
PGP: 5EAAEA16



More information about the Python-list mailing list