Skip to content

Commit 11e7dec

Browse files
committed
Updated exercises
1 parent 3b630c8 commit 11e7dec

9 files changed

Lines changed: 481 additions & 509 deletions

File tree

Instructions/Exercises/05-create-guess-the-number-game.md

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,14 @@ You'll write and run your code in Visual Studio Code. The starter code for this
4141
1. Select the `game.py` file. You'll see a set of guiding comments that act as an outline for your program — each one marks where a specific piece of code belongs:
4242
4343
```python
44+
# Add dependencies
45+
46+
4447
# Set up the game
4548
4649
# Ask the player to guess
4750
48-
# Check the guess and give feedback
51+
# Check the guess and give feedback
4952
5053
# Announce the result
5154
```
@@ -58,11 +61,16 @@ You'll write and run your code in Visual Studio Code. The starter code for this
5861
5962
Every guessing game needs a target. You'll use Python's built-in `random` module to pick a random whole number between 1 and 20.
6063
61-
1. Beneath the `# Set up the game` comment, add the following lines:
64+
1. Under the `# Add dependencies` comment, add the following line:
6265
6366
```python
6467
import random
68+
```
69+
70+
71+
1. Beneath the `# Set up the game` comment, add the following lines:
6572
73+
```python
6674
secret_number = random.randint(1, 20)
6775
max_guesses = 5
6876
guess_count = 0
@@ -89,30 +97,29 @@ Every guessing game needs a target. You'll use Python's built-in `random` module
8997
9098
## Ask the player to guess
9199
92-
Next, you'll add a `while` loop that keeps prompting the player until they either run out of guesses or get the answer right. Just like scores in the previous exercise, the player's input comes in as a string, so you convert it to an integer.
100+
Next, you'll add a `while` loop that keeps prompting the player until they either run out of guesses or get the answer right. The player's input comes in as a string, so you convert it to an integer.
93101
94102
1. Beneath the `# Ask the player to guess` comment, add the following lines:
95103
96104
```python
97105
while guess_count < max_guesses:
98106
guess_count += 1
99-
guess = int(input(f"\nGuess {guess_count}: "))
107+
guess = int(input(f"\nGuess #{guess_count}: "))
100108
```
101109
102110
- `while guess_count < max_guesses:` keeps the loop running until the player has used all their guesses.
103111
- `guess_count += 1` bumps the count each time through the loop — this is what eventually causes the condition to become `False`.
104112
- `int(input(...))` reads the player's input and converts it to a whole number in one step.
105113
106-
> **Note**: The lines beneath the `while` statement must be indented. Python uses indentation to know which lines are part of the loop.
107-
108114
## Check the guess and give feedback
109115
110116
Now the loop needs to compare the guess to the secret number and tell the player whether they were too high, too low, or exactly right. When they guess right, you use `break` to exit the loop early.
111117
112-
1. Beneath the `guess = int(...)` line, add the following code. Keep the same indentation so it stays inside the `while` loop:
118+
Be sure to maintain the correct indentation levels as you add code beneath the `while` loop.
119+
120+
1. Beneath the `# Check the guess and give feedback` comment, add the following lines:
113121
114122
```python
115-
# Check the guess and give feedback
116123
if guess == secret_number:
117124
print(f"Correct! You got it in {guess_count} guesses.")
118125
break
@@ -122,9 +129,7 @@ Now the loop needs to compare the guess to the secret number and tell the player
122129
print("Too high.")
123130
```
124131
125-
Move the `# Check the guess and give feedback` comment from the top of the file down into the loop (as shown above) — that's where the code actually lives.
126-
127-
1. Run the program. Type a guess after each prompt and press **Enter**. You should see something like:
132+
2. Run the program. Type a guess after each prompt and press **Enter**. You should see something like:
128133
129134
```output
130135
I'm thinking of a number between 1 and 20.
@@ -144,21 +149,22 @@ Now the loop needs to compare the guess to the secret number and tell the player
144149
145150
Right now, if the player runs out of guesses, the program just ends silently. You'll add a final message after the loop to reveal the secret number when the player loses.
146151
147-
1. Beneath the `# Announce the result` comment (which should be back at the bottom of your program, **not** indented inside the loop), add the following:
152+
1. Beneath the `# Announce the result` comment, add the following:
148153
149154
```python
150155
else:
151156
print(f"\nOut of guesses! The number was {secret_number}.")
152157
```
153158
154-
Python's `while` loop supports an `else` block that runs **only if the loop finished naturally** (without hitting `break`). It's a perfect fit for handling the "you lost" case.
159+
Python's `while` loop supports an `else` block that runs **only if the loop finished naturally** (without hitting `break`). It's a perfect fit for handling the "you lost" case so the player sees the secret number.
155160
156-
1. Your complete program should now look like this:
161+
2. Your complete program should now look like this:
157162
158163
```python
159-
# Set up the game
164+
# Add dependencies
160165
import random
161166
167+
# Set up the game
162168
secret_number = random.randint(1, 20)
163169
max_guesses = 5
164170
guess_count = 0
@@ -169,7 +175,7 @@ Right now, if the player runs out of guesses, the program just ends silently. Yo
169175
# Ask the player to guess
170176
while guess_count < max_guesses:
171177
guess_count += 1
172-
guess = int(input(f"\nGuess {guess_count}: "))
178+
guess = int(input(f"\nGuess #{guess_count}: "))
173179
174180
# Check the guess and give feedback
175181
if guess == secret_number:
@@ -185,26 +191,26 @@ Right now, if the player runs out of guesses, the program just ends silently. Yo
185191
print(f"\nOut of guesses! The number was {secret_number}.")
186192
```
187193
188-
1. Run the program a few times. Try guessing correctly on your first try, and also try running out of guesses on purpose to see the losing message.
194+
3. Run the program a few times. Try guessing correctly on your first try, and also try running out of guesses on purpose to see the losing message.
189195
190196
## Extend with GitHub Copilot
191197
192198
Now that the game is working, use GitHub Copilot to extend it. Open the Copilot Chat panel in VS Code (**Ctrl+Alt+I**) and try the following prompts.
193199
194200
**Handle invalid input safely**
195201
196-
> "In Python, what happens if I use `int(input())` and the user types letters instead of a number? How can I handle that safely as a beginner?"
202+
> "What happens if I use `int(input())` and the user types letters instead of a number? How can I handle that safely as a beginner?"
197203
198204
Right now, if the player types anything that isn't a whole number, the program crashes. Ask the AI to show you how to keep prompting until the player enters a valid number, then apply the pattern to your `guess = int(input(...))` line.
199205
200206
**Remember previous guesses**
201207
202-
> "In Python, how can I remember which values a user has already entered so I can warn them if they repeat one?"
208+
> "How can I remember which values a user has already entered so I can warn them if they repeat one?"
203209
204210
The current game doesn't stop the player from wasting a guess on a number they already tried. Ask the AI how to track past guesses and use its answer to add a warning that doesn't count repeat guesses against them.
205211
206212
**Give warmer hints**
207213
208-
> "In Python, how can I check whether a number is close to another number? Can you show me an example using `abs()`?"
214+
> "How can I check whether a number is close to another number? Can you show me an example using `abs()`?"
209215
210216
Currently the game only says "too high" or "too low." Ask the AI how to use `abs()` to detect when a guess is within 2 of the answer, and add a "very close" hint to your feedback logic.

0 commit comments

Comments
 (0)