5Asking Questions with if and else

Image

In programming, we often ask yes or no questions, and do something based on the answer. For example, we might ask, “Are you older than 20?” and, if the answer is yes, respond with “You are too old!” These sorts of questions are called conditions, and we combine conditions and their responses into if statements. Conditions can be more complicated than a single question, and if statements can be combined with multiple questions and different responses based on the answer to each question. In this chapter, you’ll learn to use if statements to build programs.

If Statements

We might write an if statement in Python ...

Get Python for Kids, 2nd Edition 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.