Python (Fall 2026) Module 6-5: Loops in Practice Guessing Game
Overview
Bill Moseley builds a Python number-guessing game using `random.randint`, a `while` loop, and `if`/`elif`/`else` comparisons, giving the player three guesses to find a number from 1 to 10. He then adds a remaining-guesses display and an outer replay loop, demonstrating how configurable variables and `break` statements control game flow while noting that input validation is omitted for simplicity.
Key takeaways
- Use configurable variables such as `intMin`, `intMax`, and `NG` to change a guessing game's number range and attempt limit without rewriting its core logic.
- A guess counter initialized to 0 can control a `while` loop with `G < NG`; increment it after each incorrect guess to enforce the attempt limit.
- Compare each integer guess with the secret number using `if` and `elif`, and use `break` to end the guessing loop immediately after a correct answer.
- Calculate attempts left as `NG - G` and display the result after incorrect guesses, keeping feedback tied to the same loop counter that limits play.
- Place the round setup and guessing loop inside an outer `while True` loop to support replay, then normalize the response with `.strip().lower()` and exit on `n`.
- Converting user input directly with `int()` can raise an error for nonnumeric text; this beginner example intentionally omits validation, though a more robust game should handle invalid input.
Chapters
- Bill Moseley sets `NG` to 3 and configures the secret-number range with `intMin = 1` and `intMax = 10`.
- After importing Python's `random` module, `random.randint(intMin, intMax)` selects the secret number.
- A guess counter starting at 0 controls a `while` loop; `if` and `elif` report guesses that are too low or too high, while `break` ends the loop after a correct guess.
- The example converts stripped input with `int()`, but skips checking whether the user entered a valid integer.
- After an incorrect guess, the game calculates remaining attempts as `NG - G` and displays the result inside the guessing loop.
- Changing `intMax` updates both the random-number range and the prompt, making the game easier to configure.
- Moseley demonstrates expanding the range from 1 to 10 to 1 to 20 and explains that the number of allowed guesses also affects difficulty.
- An outer `while True` loop begins before secret-number generation so each round selects a new number and resets the guess counter.
- The existing game code is indented inside the outer loop, while the final replay prompt asks whether the user wants another round.
- The response is processed with `.strip().lower()`; entering `n` triggers `break`, while other responses start a new round.
- The completed program combines nested loops, conditionals, counters, and `break` to keep playing until the user answers no.
Transcript excerpt
0:02 So, let's, let's take our guessing game. Remember the number guessing game we did a little while back? And I want to take that, and let's rebuild that. Let's rebuild that really quick. But let's use a loop so that you can give the user multiple guesses. Okay. So, let's, let's talk about this. I'm gonna do my little welcome here. Okay. So, I'm going to create a few variables that allow me to adjust the difficulty of this game kind of on the fly. So, I'm going to say int number guesses.
0:57 That's kind of a long variable name. I'm just going to call it NG for number of guesses. Why? Because there aren't there very many, there aren't very many variables in this game, and I'll be able to remember what this is. So, I'm going to try three guesses. I'm also going to, intMin number is one. intMax is 10. So basically what I'm setting up here is, I'm going to give the user three guesses to, to try and figure out a number between 1 and 10. So the first thing I want to do is I want to create that secret number. intSecret =. Now remember, we have to use random for this. So, if I want to use random, I'm going
1:52 to come up to the top and I'm going to import random so that I can have access to that library. So, I put import random up at the top. And now I can use random.randint, which allows me to pick a random integer. And this is where I'm going to plug in my variables for min, minimum and maximum. So, it's going to be between one and 10. But now I can easily adjust the difficulty of my game by changing the value of these variables. I could say between one and 100 or something like that.
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Bill Moseley, Ph.D. - Computer Science Educator.