CHAPTER 5

image

Guessing Games with Random Numbers and Loops

The last step before we start with graphics is learning how to loop a section of code. Most games loop. They repeat the same code over and over. For example, the number guessing game below loops for each guess that the user makes:

Hi! I'm thinking of a random number between 1 and 100.--- Attempt 1Guess what number I am thinking of: 50Too high.--- Attempt 2Guess what number I am thinking of: 25Too high.--- Attempt 3Guess what number I am thinking of: 17Too high.--- Attempt 4Guess what number I am thinking of: 9Too low.--- Attempt 5Guess what number I am thinking of: 14Too high.--- Attempt ...

Get Program Arcade Games: With Python and Pygame, Fourth 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.