Chapter 7Guess the Number

Image

In Chapter 6 we looked at for loops. In this chapter, we’ll look at conditional loops and use one to build a Guess The Number game.

Conditional Loops

You now know how to use for loops. As a reminder, these are used to loop through a finite set of options.

Conditional loops loop based on a condition. They are the more powerful and flexible type of loops, and they also tend to be the one you’ll use most.

Let’s start with a simple (and useless) example. Create file Loop4.py and type the following:

# Get some input
userInput=input("Say something, say STOP to stop: ").upper().strip()

# Loop ...

Get Captain Code: Unleash Your Coding Superpower with Python 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.