Puzzle 15 Where’s Waldo?

Python Brain Teasers — by Miki Tebeka (24 / 40)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 The Great Divide | TOC | Call Me Maybe 👉

waldo.py

​ name = ​'Waldo'​
​ text = ​'Can you find where Wally is?'​

​ ​if​ text.find(name):
​ ​print​(​'Found Waldo'​)
​ ​else​:
​ ​print​(​'Cannot find Waldo'​)

Guess the Output

IMPORTANT

Try to guess what the output is before moving to the next page.

images/hline.png

This code will print: Found Waldo

images/hline.png

The str.find documentation says

Return -1 if sub is not found.

We have two Boolean values in Python: True and False. They weren’t always there; they were added in Python 2.3.

How can you do logical operations without True and False? There are rules! Everything is True except

  • 0 numbers: 0, 0.0, 0+0j, …
  • Empty collections: [], {}, ”, …
  • None
  • False

You can test the truth value of a Python object using the built-in bool function.

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.