|
| 1 | +--- |
| 2 | +lab: |
| 3 | + title: 'Create a personalized banner app' |
| 4 | + description: 'Set up a Python project in Visual Studio Code, install a package with pip, and use GitHub Copilot to build a banner app that greets the user.' |
| 5 | + level: 100 |
| 6 | + duration: 25 |
| 7 | + islab: true |
| 8 | + status: 'released' |
| 9 | +--- |
| 10 | + |
| 11 | +# Create a personalized banner app |
| 12 | + |
| 13 | +In this exercise, you take everything you set up in this module for a test drive. You create a new Python project in Visual Studio Code, set up a virtual environment, install a package with `pip`, and write a small program that turns the user's name into an eye-catching ASCII banner. Along the way, you practice reading an error message and getting help from GitHub Copilot. |
| 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. The starter code for this exercise lives in a GitHub repository — you'll clone that repo and work inside the folder for this exercise. |
| 20 | + |
| 21 | +1. Open a new **Visual Studio Code** window (**File > New Window**). |
| 22 | + |
| 23 | +1. On the Welcome page, select **Clone Git Repository...** (or open the Command Palette with **Ctrl+Shift+P** and run **Git: Clone**). |
| 24 | + |
| 25 | +1. Paste the following URL and press **Enter**: |
| 26 | + |
| 27 | + ``` |
| 28 | + https://github.com/MicrosoftLearning/mslearn-python-programming.git |
| 29 | + ``` |
| 30 | +
|
| 31 | +1. When the file selection dialog appears, create a new folder in a convenient location to hold the repo (for example, `mslearn-python`), select it, and click **Select as Repository Destination**. |
| 32 | +
|
| 33 | +1. After the clone completes, select **Open** to open the folder in VS Code. |
| 34 | +
|
| 35 | +1. In the File Explorer, expand the `Labfiles/03-create-personalized-banner` subfolder and select the `app.py` file. This is where you'll write your code. |
| 36 | +
|
| 37 | +## Set up your Python environment |
| 38 | +
|
| 39 | +Every Python project should have its own virtual environment so its packages stay separate from your other work. |
| 40 | +
|
| 41 | +1. Open the integrated VS Code terminal by selecting **Terminal > New Terminal**. Make sure the terminal is set to your project folder — you should see a path ending in `03-create-personalized-banner`. |
| 42 | +
|
| 43 | +
|
| 44 | +1. In the terminal, create a virtual environment named `.venv`: |
| 45 | +
|
| 46 | + ```bash |
| 47 | + python -m venv .venv |
| 48 | + ``` |
| 49 | +
|
| 50 | + A new hidden folder called `.venv` appears in the Explorer panel. |
| 51 | +
|
| 52 | +1. Activate the environment: |
| 53 | +
|
| 54 | + | Platform | Command | |
| 55 | + |---|---| |
| 56 | + | Windows | `.venv\Scripts\activate` | |
| 57 | + | macOS / Linux | `source .venv/bin/activate` | |
| 58 | +
|
| 59 | + When the environment is active, the terminal prompt starts with `(.venv)`. |
| 60 | +
|
| 61 | +2. VS Code should automatically detect the environment and offer to use it as the interpreter. |
| 62 | +
|
| 63 | +### Troubleshooting |
| 64 | +
|
| 65 | +**Interpreter not detected:** If VS Code doesn't detect the virtual environment: |
| 66 | + - Press **Ctrl+Shift+P** (Windows) or **Cmd+Shift+P** (macOS) to open the Command Palette. |
| 67 | + - Type `Python: Select Interpreter` and select it. |
| 68 | + - Choose the interpreter path that includes `.venv`. |
| 69 | +
|
| 70 | +**Script disabled (Windows):** If you see an error saying "script execution is disabled on this system," try the following: |
| 71 | +
|
| 72 | +1. Seach for "PowerShell" in the Start menu, right-click it, and select **Run as Administrator**. |
| 73 | +2. In the PowerShell window, run: |
| 74 | +
|
| 75 | + ```powershell |
| 76 | + Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process |
| 77 | + ``` |
| 78 | +3. Close the PowerShell window and return to VS Code. In the terminal, try activating your virtual environment again. |
| 79 | +
|
| 80 | +## Install a package with pip |
| 81 | +
|
| 82 | +Python's standard library is powerful, but real projects almost always use third-party packages too. You'll install **pyfiglet** — a small package that turns plain text into ASCII-art banners. |
| 83 | +
|
| 84 | +1. With the virtual environment active, run: |
| 85 | +
|
| 86 | + ```bash |
| 87 | + pip install pyfiglet |
| 88 | + ``` |
| 89 | +
|
| 90 | + You should see a line like `Successfully installed pyfiglet-...`. |
| 91 | +
|
| 92 | +1. Confirm the package is installed: |
| 93 | +
|
| 94 | + ```bash |
| 95 | + pip list |
| 96 | + ``` |
| 97 | +
|
| 98 | + You should see `pyfiglet` in the list, alongside `pip` itself. |
| 99 | +
|
| 100 | +## Review the starter code |
| 101 | +
|
| 102 | +1. In the **Explorer** panel, open `app.py`. The `.py` extension tells VS Code this is a Python file. You'll see the following guiding comments already in place: |
| 103 | +
|
| 104 | + ```python |
| 105 | + # Load the pyfiglet package |
| 106 | +
|
| 107 | + # Ask the user for their name |
| 108 | +
|
| 109 | + # Turn the name into a banner |
| 110 | +
|
| 111 | + # Print a greeting |
| 112 | + ``` |
| 113 | +
|
| 114 | + Remember, comments are ignored by Python when the program runs. They just help you organize your code. In the steps that follow, you'll add code beneath each comment. |
| 115 | +
|
| 116 | +## Load the package into your program |
| 117 | +
|
| 118 | +Installing a package puts the code on your machine. To actually use it in your program, you tell Python to **import** it. |
| 119 | +
|
| 120 | +1. Beneath the `# Load the pyfiglet package` comment, add the following line: |
| 121 | +
|
| 122 | + ```python |
| 123 | + import pyfiglet |
| 124 | + ``` |
| 125 | +
|
| 126 | + The `import` statement loads the package so you can use the functions inside it. |
| 127 | +
|
| 128 | +## Ask the user for their name |
| 129 | +
|
| 130 | +You've done this before in earlier modules. You'll capture the name in a variable and clean up any extra spaces the user might type. |
| 131 | +
|
| 132 | +1. Beneath the `# Ask the user for their name` comment, add the following lines: |
| 133 | +
|
| 134 | + ```python |
| 135 | + name = input("What is your name? ") |
| 136 | + name = name.strip() |
| 137 | + ``` |
| 138 | +
|
| 139 | + The `.strip()` method removes any spaces at the start or end of the text. |
| 140 | +
|
| 141 | +## Turn the name into a banner |
| 142 | +
|
| 143 | +The pyfiglet package has a function called `figlet_format()` that takes a string and returns a big ASCII version of it. |
| 144 | +
|
| 145 | +1. Beneath the `# Turn the name into a banner` comment, add the following line: |
| 146 | +
|
| 147 | + ```python |
| 148 | + banner = pyfiglet.figlet_format(name) |
| 149 | + ``` |
| 150 | +
|
| 151 | + - `pyfiglet.figlet_format(name)` calls the `figlet_format` function that lives inside the `pyfiglet` package. |
| 152 | + - The **return value** — the finished ASCII banner — is stored in the `banner` variable. |
| 153 | +
|
| 154 | +## Print a greeting |
| 155 | +
|
| 156 | +Now put it all together and display the banner along with a friendly message. |
| 157 | +
|
| 158 | +1. Beneath the `# Print a greeting` comment, add the following lines: |
| 159 | +
|
| 160 | + ```python |
| 161 | + print(banner) |
| 162 | + print(f"Hello, {name.upper()}! Welcome to VS Code.") |
| 163 | + ``` |
| 164 | +
|
| 165 | + - `print(banner)` displays the multi-line ASCII banner. |
| 166 | + - `name.upper()` converts the name to uppercase inside the f-string, without changing the original `name` variable. |
| 167 | +
|
| 168 | +1. Your complete `app.py` should now look like this: |
| 169 | +
|
| 170 | + ```python |
| 171 | + # Load the pyfiglet package |
| 172 | + import pyfiglet |
| 173 | +
|
| 174 | + # Ask the user for their name |
| 175 | + name = input("What is your name? ") |
| 176 | + name = name.strip() |
| 177 | +
|
| 178 | + # Turn the name into a banner |
| 179 | + banner = pyfiglet.figlet_format(name) |
| 180 | +
|
| 181 | + # Print a greeting |
| 182 | + print(banner) |
| 183 | + print(f"Hello, {name.upper()}! Welcome to VS Code.") |
| 184 | + ``` |
| 185 | +
|
| 186 | +## Run the program |
| 187 | +
|
| 188 | +1. Select the **▶ Run Python File** button in the top-right corner of the editor, or run it from the terminal: |
| 189 | +
|
| 190 | + ```bash |
| 191 | + python app.py |
| 192 | + ``` |
| 193 | +
|
| 194 | +1. When prompted, type your name and press **Enter**. You should see something like: |
| 195 | +
|
| 196 | + ```output |
| 197 | + What is your name? Ada |
| 198 | +
|
| 199 | + _ _ |
| 200 | + / \ __| | __ _ |
| 201 | + / _ \ / _` |/ _` | |
| 202 | + / ___ \| (_| | (_| | |
| 203 | + /_/ \_\\__,_|\__,_| |
| 204 | +
|
| 205 | + Hello, ADA! Welcome to VS Code. |
| 206 | + ``` |
| 207 | +
|
| 208 | +## Read an error message |
| 209 | +
|
| 210 | +Errors are a normal part of programming. Learning to read them is one of the most important skills you can build early on. |
| 211 | +
|
| 212 | +1. On purpose, change `name.upper()` in the last `print()` line to `nam.upper()` — remove the `e` from `name`: |
| 213 | +
|
| 214 | + ```python |
| 215 | + print(f"Hello, {nam.upper()}! Welcome to VS Code.") |
| 216 | + ``` |
| 217 | +
|
| 218 | +1. Run the program again. Type your name at the prompt. This time you should see an error at the bottom of the terminal: |
| 219 | +
|
| 220 | + ```output |
| 221 | + Traceback (most recent call last): |
| 222 | + File "app.py", line 11, in <module> |
| 223 | + print(f"Hello, {nam.upper()}! Welcome to VS Code.") |
| 224 | + NameError: name 'nam' is not defined |
| 225 | + ``` |
| 226 | +
|
| 227 | +1. Read the error carefully. It tells you: |
| 228 | + - **What went wrong** — `NameError: name 'nam' is not defined`. |
| 229 | + - **Where it happened** — `File "app.py", line 11`. |
| 230 | +
|
| 231 | + Python couldn't find a variable called `nam` because you defined it as `name`. |
| 232 | +
|
| 233 | +1. Fix the typo by putting the `e` back: |
| 234 | +
|
| 235 | + ```python |
| 236 | + print(f"Hello, {name.upper()}! Welcome to VS Code.") |
| 237 | + ``` |
| 238 | +
|
| 239 | +1. Run the program one more time to confirm everything works again. |
| 240 | +
|
| 241 | +## Extend your program with GitHub Copilot |
| 242 | +
|
| 243 | +GitHub Copilot can help you build on top of what you already have. You'll use a comment to guide Copilot into adding a farewell banner. |
| 244 | +
|
| 245 | +1. At the bottom of your `app.py`, on a new line, type the following comment and press **Enter**: |
| 246 | +
|
| 247 | + ```python |
| 248 | + # Ask the user how they're feeling today and print their answer as a banner |
| 249 | + ``` |
| 250 | +
|
| 251 | +1. Wait a moment. Copilot should display a suggestion in gray text — something similar to: |
| 252 | +
|
| 253 | + ```python |
| 254 | + feeling = input("How are you feeling today? ") |
| 255 | + print(pyfiglet.figlet_format(feeling)) |
| 256 | + ``` |
| 257 | +
|
| 258 | +1. Press **Tab** to accept the suggestion. If Copilot doesn't offer one, press **Enter** once and start typing `feeling = input(` — the suggestion should appear as you type. |
| 259 | +
|
| 260 | + > [!IMPORTANT] |
| 261 | + > Always **read** what Copilot suggests before you accept it. In this case, the suggestion should use `pyfiglet.figlet_format()` — the same function you already used above. If it suggests something completely different, keep typing to guide it or reject the suggestion and write the line yourself. |
| 262 | +
|
| 263 | +1. Run the program one more time and answer both prompts. You should see two banners printed back to back. |
| 264 | +
|
| 265 | +## Save your project's dependencies |
| 266 | +
|
| 267 | +If you ever share this project — or come back to it on another machine — you'll want a record of which packages it needs. That's what `requirements.txt` is for. |
| 268 | +
|
| 269 | +1. In the terminal, run: |
| 270 | +
|
| 271 | + ```bash |
| 272 | + pip freeze > requirements.txt |
| 273 | + ``` |
| 274 | +
|
| 275 | +1. In the Explorer panel, open the new `requirements.txt` file. You should see a single line similar to: |
| 276 | +
|
| 277 | + ```output |
| 278 | + pyfiglet==1.0.2 |
| 279 | + ``` |
| 280 | +
|
| 281 | + Anyone who opens your project can now recreate the exact environment with `pip install -r requirements.txt`. |
| 282 | +
|
| 283 | +## Extend with GitHub Copilot |
| 284 | +
|
| 285 | +Now that the banner app is working, use GitHub Copilot to extend it. Open the Copilot Chat panel in VS Code (**Ctrl+Alt+I**) and try the following prompts. |
| 286 | +
|
| 287 | +**Try a different banner font** |
| 288 | +
|
| 289 | +> "I'm using the pyfiglet package in Python. How do I display text in a different font?" |
| 290 | +
|
| 291 | +The banner currently uses the default pyfiglet font. Ask the AI about the `font` argument for `figlet_format()`, then update your program to display the banner in a different style — `"slant"`, `"big"`, and `"bubble"` are good ones to start with. |
| 292 | +
|
| 293 | +**Center your greeting** |
| 294 | +
|
| 295 | +> "In Python, how can I center a string inside a wider space when I print it?" |
| 296 | +
|
| 297 | +Right now the `Hello, ...` line sits flush against the left margin, below a wide banner. Ask the AI about the string `.center()` method, then use it to center your greeting inside a 40-character space so it lines up more nicely. |
| 298 | +
|
| 299 | +**Ask Copilot to explain your code** |
| 300 | +
|
| 301 | +> Open your `app.py`, select the entire program, and ask Copilot Chat: "Explain what this code does, line by line." |
| 302 | +
|
| 303 | +Read Copilot's response carefully and compare it to your own understanding. If any line's explanation surprises you, ask a follow-up question (for example, *"Why do we need `.strip()` on the user's name?"*). Using Copilot as a study partner — not just an autocomplete tool — is one of the fastest ways to build understanding as a beginner. |
0 commit comments