Skip to content

Commit 7ee4ef9

Browse files
committed
Added exercises for part 2
1 parent b17b33e commit 7ee4ef9

4 files changed

Lines changed: 1007 additions & 0 deletions

File tree

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
---
2+
lab:
3+
title: 'Create a Guess the Number game'
4+
description: 'Write a Python program that uses a while loop, conditional logic, and break to run an interactive Guess the Number game.'
5+
level: 100
6+
duration: 25
7+
islab: true
8+
status: 'released'
9+
---
10+
11+
# Create a Guess the Number game
12+
13+
In this exercise, you build a classic Guess the Number game. The computer picks a secret number, and the player keeps guessing until they get it right. You practice using a `while` loop to repeat actions, `if/elif/else` to compare values, and `break` to exit the loop when the player wins.
14+
15+
This exercise takes approximately **25** minutes.
16+
17+
## Set up your workspace
18+
19+
You'll write and run your code in Visual Studio Code.
20+
21+
1. Open **Visual Studio Code**.
22+
23+
1. Select **File > Open Folder**, create a new folder called `guess-the-number` somewhere on your machine, and open it.
24+
25+
1. In the **Explorer** panel, select the **New File** icon and name the file `game.py`.
26+
27+
1. Make sure your Python environment is active. You should see a Python version displayed in the bottom status bar. If you see a warning, select it and choose your installed Python interpreter.
28+
29+
## Set up your starter code
30+
31+
Before writing any logic, you'll paste a set of guiding comments into `game.py`. These comments act as an outline for your program — each one marks where a specific piece of code belongs.
32+
33+
1. Copy the following comments and paste them into `game.py`:
34+
35+
```python
36+
# Set up the game
37+
38+
# Ask the player to guess
39+
40+
# Check the guess and give feedback
41+
42+
# Announce the result
43+
```
44+
45+
Remember, comments are ignored by Python when the program runs. They're just there to help you organize your code.
46+
47+
1. Leave the code as-is for now. In the steps that follow, you'll add code beneath each comment.
48+
49+
## Pick a secret number
50+
51+
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.
52+
53+
1. Beneath the `# Set up the game` comment, add the following lines:
54+
55+
```python
56+
import random
57+
58+
secret_number = random.randint(1, 20)
59+
max_guesses = 5
60+
guess_count = 0
61+
62+
print("I'm thinking of a number between 1 and 20.")
63+
print(f"You have {max_guesses} guesses. Good luck!")
64+
```
65+
66+
- `random.randint(1, 20)` returns a random whole number **including** both 1 and 20.
67+
- `max_guesses` and `guess_count` will control how the loop runs.
68+
69+
1. Select the **▶ Run Python File** button in the top-right corner of the editor, or open the terminal (**Terminal > New Terminal**) and run:
70+
71+
```bash
72+
python game.py
73+
```
74+
75+
You should see something like:
76+
77+
```output
78+
I'm thinking of a number between 1 and 20.
79+
You have 5 guesses. Good luck!
80+
```
81+
82+
## Ask the player to guess
83+
84+
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.
85+
86+
1. Beneath the `# Ask the player to guess` comment, add the following lines:
87+
88+
```python
89+
while guess_count < max_guesses:
90+
guess_count += 1
91+
guess = int(input(f"\nGuess {guess_count}: "))
92+
```
93+
94+
- `while guess_count < max_guesses:` keeps the loop running until the player has used all their guesses.
95+
- `guess_count += 1` bumps the count each time through the loop — this is what eventually causes the condition to become `False`.
96+
- `int(input(...))` reads the player's input and converts it to a whole number in one step.
97+
98+
> **Note**: The lines beneath the `while` statement must be indented. Python uses indentation to know which lines are part of the loop.
99+
100+
## Check the guess and give feedback
101+
102+
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.
103+
104+
1. Beneath the `guess = int(...)` line, add the following code. Keep the same indentation so it stays inside the `while` loop:
105+
106+
```python
107+
# Check the guess and give feedback
108+
if guess == secret_number:
109+
print(f"Correct! You got it in {guess_count} guesses.")
110+
break
111+
elif guess < secret_number:
112+
print("Too low.")
113+
else:
114+
print("Too high.")
115+
```
116+
117+
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.
118+
119+
1. Run the program. Type a guess after each prompt and press **Enter**. You should see something like:
120+
121+
```output
122+
I'm thinking of a number between 1 and 20.
123+
You have 5 guesses. Good luck!
124+
125+
Guess 1: 10
126+
Too low.
127+
128+
Guess 2: 15
129+
Too high.
130+
131+
Guess 3: 12
132+
Correct! You got it in 3 guesses.
133+
```
134+
135+
## Announce the result
136+
137+
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.
138+
139+
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:
140+
141+
```python
142+
else:
143+
print(f"\nOut of guesses! The number was {secret_number}.")
144+
```
145+
146+
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.
147+
148+
1. Your complete program should now look like this:
149+
150+
```python
151+
# Set up the game
152+
import random
153+
154+
secret_number = random.randint(1, 20)
155+
max_guesses = 5
156+
guess_count = 0
157+
158+
print("I'm thinking of a number between 1 and 20.")
159+
print(f"You have {max_guesses} guesses. Good luck!")
160+
161+
# Ask the player to guess
162+
while guess_count < max_guesses:
163+
guess_count += 1
164+
guess = int(input(f"\nGuess {guess_count}: "))
165+
166+
# Check the guess and give feedback
167+
if guess == secret_number:
168+
print(f"Correct! You got it in {guess_count} guesses.")
169+
break
170+
elif guess < secret_number:
171+
print("Too low.")
172+
else:
173+
print("Too high.")
174+
175+
# Announce the result
176+
else:
177+
print(f"\nOut of guesses! The number was {secret_number}.")
178+
```
179+
180+
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.
181+
182+
## Extend with GitHub Copilot
183+
184+
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.
185+
186+
**Handle invalid input safely**
187+
188+
> "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?"
189+
190+
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.
191+
192+
**Remember previous guesses**
193+
194+
> "In Python, how can I remember which values a user has already entered so I can warn them if they repeat one?"
195+
196+
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.
197+
198+
**Give warmer hints**
199+
200+
> "In Python, how can I check whether a number is close to another number? Can you show me an example using `abs()`?"
201+
202+
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)