โ†Previous page

06TRUE OR FALSE? ยท CHALLENGES

Logic challenges: be a detective.

Mix comparisons, and, or, not, and in to solve small mysteries. Parentheses keep long questions clear.

IN REAL LIFE

Detectives think in True and False.

Each clue is a small question. Together they solve the case.

Clues

Has crumbs? Was at home? Each clue is True or False.

Ride rules

You ride if you are tall enough and you have a ticket.

Secret club

You get in if you know the password and your name is on the list.

LOOK FIRST

The order Python follows.

Parentheses first

Whatever is inside ( ) is answered before anything else.

Then compare and search

Comparisons like age > 8 and searches like "a" in word become True or False.

Then not

not flips the answer right next to it.

and, and last of all or

and goes before or. True or False and False is True, because False and False is done first.

A trick: When a question gets long, add ( ). Then you and Python read it the same way.

STEP BY STEP

Case: who ate the cake?

Follow the clues one at a time, like Python.

has_crumbs = TrueMax has crumbs on the shirt.
was_home = TrueMax was at home, next to the cake.
has_alibi = FalseNobody saw Max somewhere else.
if has_crumbs and was_home and not has_alibi:not has_alibi: flip False into True. Then True and True and True: all True.
print("Max ate the cake!")Every clue agrees, so this path runs. Case closed!

RUN IT

Who ate the cookie?

Guess first. Then run real Python and check.

1

Guess the result

name = "Leo" crumbs = ["Leo", "Mia"] at_park = ["Mia", "Ana"] if name in crumbs and name not in at_park: print(name + " ate the cookie!") else: print(name + " is innocent")

What will Python show?

detective.pyREAL PYTHON
OUTPUT
Ready. Press Run Python.
Python runs here thanks to Pyodide โ™ฅ

Pyodide 314.0.3 runs CPython in your browser with WebAssembly.

Open-source under the Mozilla Public License 2.0. License

Now change one thing: Change name to "Mia". Is Mia innocent? Then try "Ana".

YOUR TURN

Solve the case.

Try each tiny question. A wrong answer becomes something to review.

0Completed
9Not tried
0Review
01

Who is the thief?

Clue: the thief wears a hat and does NOT wear glasses. Sam: hat, glasses. Ana: hat, no glasses. Leo: no hat, no glasses.

Who matches both clues?

02

Between 8 and 10

age = 9 age >= 8 and age <= 10

What does Python answer?

03

and goes first

True or False and False

What does Python answer?

04

Now with ( )

(True or False) and False

What does Python answer?

05

Pet check

pets = ["cat", "dog"] "cat" in pets and "fish" not in pets

What does Python answer?

06

The secret door

password = "pybot" day = "Monday" vip = True password == "pybot" and (day == "Saturday" or vip)

Does the door open? (What does Python answer?)

07

Same question, new words

not (age < 8)

Which one always gives the same answer?

08

Fix PyBot's code

The ride is closed today, but PyBot's code still lets a small kid in. and goes before or. Add ( ) so the ride must be open.

Goal output
closed today
OUTPUT

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

Big questions are small questions joined together.

Next, you will pack it all inside a box with a name.