You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Instructions/Exercises/05-create-guess-the-number-game.md
+26-20Lines changed: 26 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -41,11 +41,14 @@ You'll write and run your code in Visual Studio Code. The starter code for this
41
41
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:
42
42
43
43
```python
44
+
# Add dependencies
45
+
46
+
44
47
# Set up the game
45
48
46
49
# Ask the player to guess
47
50
48
-
# Check the guess and give feedback
51
+
# Check the guess and give feedback
49
52
50
53
# Announce the result
51
54
```
@@ -58,11 +61,16 @@ You'll write and run your code in Visual Studio Code. The starter code for this
58
61
59
62
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.
60
63
61
-
1. Beneath the `# Set up the game` comment, add the following lines:
64
+
1. Under the `# Add dependencies` comment, add the following line:
62
65
63
66
```python
64
67
import random
68
+
```
69
+
70
+
71
+
1. Beneath the `# Set up the game` comment, add the following lines:
65
72
73
+
```python
66
74
secret_number = random.randint(1, 20)
67
75
max_guesses = 5
68
76
guess_count = 0
@@ -89,30 +97,29 @@ Every guessing game needs a target. You'll use Python's built-in `random` module
89
97
90
98
## Ask the player to guess
91
99
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.
93
101
94
102
1. Beneath the `# Ask the player to guess` comment, add the following lines:
95
103
96
104
```python
97
105
while guess_count < max_guesses:
98
106
guess_count += 1
99
-
guess = int(input(f"\nGuess {guess_count}: "))
107
+
guess = int(input(f"\nGuess #{guess_count}: "))
100
108
```
101
109
102
110
- `while guess_count < max_guesses:` keeps the loop running until the player has used all their guesses.
103
111
- `guess_count += 1` bumps the count each time through the loop — this is what eventually causes the condition to become `False`.
104
112
- `int(input(...))` reads the player's input and converts it to a whole number in one step.
105
113
106
-
> **Note**: The lines beneath the `while` statement must be indented. Python uses indentation to know which lines are part of the loop.
107
-
108
114
## Check the guess and give feedback
109
115
110
116
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.
111
117
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:
113
121
114
122
```python
115
-
# Check the guess and give feedback
116
123
if guess == secret_number:
117
124
print(f"Correct! You got it in {guess_count} guesses.")
118
125
break
@@ -122,9 +129,7 @@ Now the loop needs to compare the guess to the secret number and tell the player
122
129
print("Too high.")
123
130
```
124
131
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:
128
133
129
134
```output
130
135
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
144
149
145
150
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.
146
151
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:
148
153
149
154
```python
150
155
else:
151
156
print(f"\nOut of guesses! The number was {secret_number}.")
152
157
```
153
158
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.
155
160
156
-
1. Your complete program should now look like this:
161
+
2. Your complete program should now look like this:
157
162
158
163
```python
159
-
# Set up the game
164
+
# Add dependencies
160
165
import random
161
166
167
+
# Set up the game
162
168
secret_number = random.randint(1, 20)
163
169
max_guesses = 5
164
170
guess_count = 0
@@ -169,7 +175,7 @@ Right now, if the player runs out of guesses, the program just ends silently. Yo
169
175
# Ask the player to guess
170
176
while guess_count < max_guesses:
171
177
guess_count += 1
172
-
guess = int(input(f"\nGuess {guess_count}: "))
178
+
guess = int(input(f"\nGuess #{guess_count}: "))
173
179
174
180
# Check the guess and give feedback
175
181
if guess == secret_number:
@@ -185,26 +191,26 @@ Right now, if the player runs out of guesses, the program just ends silently. Yo
185
191
print(f"\nOut of guesses! The number was {secret_number}.")
186
192
```
187
193
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.
189
195
190
196
## Extend with GitHub Copilot
191
197
192
198
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.
193
199
194
200
**Handle invalid input safely**
195
201
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?"
197
203
198
204
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.
199
205
200
206
**Remember previous guesses**
201
207
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?"
203
209
204
210
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.
205
211
206
212
**Give warmer hints**
207
213
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()`?"
209
215
210
216
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