Chapter 3. 3 booleans, decisions, and loops: Decisive Code

Image

Have you noticed how, so far, our programs aren’t very, well, interesting? That is, all our code has strictly been a set of statements the interpreter evaluates from top to bottom—no twists in the plot, no sudden turns, no surprises, no independent thinking. For code to be more interesting, it needs to make decisions, to control its own destiny, and to do things more than once straight through. And in this chapter that’s exactly what we’re going to learn to do. Along the way we’ll learn about the mysterious game called shoushiling, meet a character named Boole, and see how a data type with only two values could be worth our time. We’re even going to learn how to deal with the dreaded infinite loop. Let’s get started!

Note

We may even create one just for the fun of it!

Would you like to play a game?

Passed down from the ancient Chinese Han dynasty, the game shoushiling has been used to settle court case decisions, to decide multimillion-dollar deals, and perhaps most importantly, to determine who gets to sit in the front seat of the car.

Today you know the game as Rock, Paper, Scissors, and we’re going to implement it so that you can play the game against a rather tough opponent: your computer.

Image

How Rock, Paper, Scissors ...

Get Head First Learn to Code now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.