4REPEATING CODE: INDEFINITE LOOPS

image

The for loops and range for loops that you learned in Chapter 3 are convenient for looping through a string or range of indices. But what do we do when we have no string or when the indices do not follow a fixed pattern? We use a while loop, the topic of this chapter. while loops are more general than for loops and can handle situations that a for loop cannot.

We’ll solve three problems where for loops fall short: determining the number of times slot machines can be played, organizing a song playlist until the user wants to stop, and decoding an encoded message.

Problem #8: Slot Machines

How many times can slot ...

Get Learn to Code by Solving Problems 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.