|
| 1 | +--- |
| 2 | +lab: |
| 3 | + title: 'Create a personalized greeting' |
| 4 | + description: 'Write a Python program that uses print(), input(), and f-strings to display a personalized greeting message.' |
| 5 | + level: 100 |
| 6 | + duration: 15 |
| 7 | + islab: true |
| 8 | + status: 'released' |
| 9 | +--- |
| 10 | + |
| 11 | +# Create a personalized greeting |
| 12 | + |
| 13 | +In this exercise, you write a Python program that asks for the user's name and displays a personalized greeting. You practice using the `print()` function to display output, the `input()` function to collect user input, and f-strings to format a custom message. |
| 14 | + |
| 15 | +This exercise takes approximately **15** minutes. |
| 16 | + |
| 17 | +## Open the online Python IDE |
| 18 | + |
| 19 | +You'll write and run your code using an online Python editor — no installation required. |
| 20 | + |
| 21 | +1. Open a browser and navigate to the Python editor at [https://buzahid.github.io/python-coder/app/](https://buzahid.github.io/python-coder/app/). |
| 22 | + |
| 23 | +1. You'll see two panels: |
| 24 | + - **Editor pane** (left): where you write your Python code. |
| 25 | + - **Output terminal** (right): where output is displayed and where you can type input when the program asks for it. |
| 26 | + |
| 27 | +1. The editor may contain some default code. Select all of it and delete it so you're starting with a clean, empty file. |
| 28 | + |
| 29 | +## Display a welcome message |
| 30 | + |
| 31 | +The first thing your program should do is greet the user when it starts. You'll use the `print()` function to display a message on the screen. |
| 32 | + |
| 33 | +1. In the editor pane, type the following code: |
| 34 | + |
| 35 | + ```python |
| 36 | + print("Welcome to the greeting program!") |
| 37 | + ``` |
| 38 | + |
| 39 | +1. Select the **▶ (Run Code)** button to run the code. |
| 40 | + |
| 41 | +1. Check the output terminal. You should see: |
| 42 | + |
| 43 | + ```output |
| 44 | + Welcome to the greeting program! |
| 45 | + ``` |
| 46 | + |
| 47 | + > **Note**: If nothing appears, make sure the code is typed exactly as shown, including the quotes and parentheses. |
| 48 | + |
| 49 | +## Ask for the user's name |
| 50 | + |
| 51 | +Now you'll update the program to pause and ask for the user's name. The `input()` function displays a prompt and waits for the user to type a response. |
| 52 | + |
| 53 | +1. Add the following line below your existing code: |
| 54 | + |
| 55 | + ```python |
| 56 | + name = input("What is your name? ") |
| 57 | + ``` |
| 58 | + |
| 59 | + This line does two things: it displays the prompt `What is your name? ` in the terminal, and it stores whatever the user types into a variable called `name`. |
| 60 | + |
| 61 | +1. Run the program again by selecting **▶**. |
| 62 | + |
| 63 | +1. When `What is your name? ` appears in the output terminal, click on the terminal and type your name, then press **Enter**. |
| 64 | + |
| 65 | +1. The program finishes after you enter your name — but it doesn't say anything yet. You'll fix that in the next step. |
| 66 | + |
| 67 | +## Display a personalized greeting |
| 68 | + |
| 69 | +Now you'll use the `name` variable to build a personalized message. You'll do this with an **f-string** — a string that can embed variable values directly inside it using curly braces `{}`. |
| 70 | + |
| 71 | +1. Add the following line at the end of your code: |
| 72 | + |
| 73 | + ```python |
| 74 | + print(f"Hello, {name}! It's great to meet you.") |
| 75 | + ``` |
| 76 | + |
| 77 | +1. Your complete program should now look like this: |
| 78 | + |
| 79 | + ```python |
| 80 | + print("Welcome to the greeting program!") |
| 81 | + name = input("What is your name? ") |
| 82 | + print(f"Hello, {name}! It's great to meet you.") |
| 83 | + ``` |
| 84 | + |
| 85 | +1. Run the program, enter your name when prompted, and press **Enter**. |
| 86 | + |
| 87 | +1. You should see output similar to: |
| 88 | + |
| 89 | + ```output |
| 90 | + Welcome to the greeting program! |
| 91 | + What is your name? Alex |
| 92 | + Hello, Alex! It's great to meet you. |
| 93 | + ``` |
| 94 | + |
| 95 | +## Extend the greeting |
| 96 | + |
| 97 | +Let's make the greeting more personal by also asking for the user's favorite color and including it in the message. |
| 98 | + |
| 99 | +1. Add two more lines below your existing code to ask for a favorite color and include it in a second message: |
| 100 | + |
| 101 | + ```python |
| 102 | + color = input("What is your favorite color? ") |
| 103 | + print(f"Great choice! {color} is a wonderful color.") |
| 104 | + ``` |
| 105 | + |
| 106 | +1. Your complete program should now look like this: |
| 107 | + |
| 108 | + ```python |
| 109 | + print("Welcome to the greeting program!") |
| 110 | + name = input("What is your name? ") |
| 111 | + print(f"Hello, {name}! It's great to meet you.") |
| 112 | + color = input("What is your favorite color? ") |
| 113 | + print(f"Great choice! {color} is a wonderful color.") |
| 114 | + ``` |
| 115 | + |
| 116 | +1. Run the program and respond to both prompts. |
| 117 | + |
| 118 | +1. Verify the output matches what you entered. For example: |
| 119 | + |
| 120 | + ```output |
| 121 | + Welcome to the greeting program! |
| 122 | + What is your name? Alex |
| 123 | + Hello, Alex! It's great to meet you. |
| 124 | + What is your favorite color? Blue |
| 125 | + Great choice! Blue is a wonderful color. |
| 126 | + ``` |
| 127 | + |
| 128 | +## Code with AI |
| 129 | + |
| 130 | +Using an AI assistant (like Copilot) is great way to explore a programming language at your own pace. Try each prompt below, read the response carefully, and test the code examples it gives you in the online editor. |
| 131 | + |
| 132 | +### Discovery 1: Create a decorative border |
| 133 | + |
| 134 | +**AI Prompt:** |
| 135 | +> "In Python, how can I print 20 dashes in a row without typing them all out? Can you show me an example?" |
| 136 | + |
| 137 | +**After the AI responds:** String repetition is a quick way to draw dividers or format output. Try running any examples the AI gives you in the online editor, and experiment with different characters and lengths. |
| 138 | + |
| 139 | +<details> |
| 140 | +<summary>Show answer</summary> |
| 141 | +You can use string multiplication to repeat a character multiple times. For example, to print 20 dashes in a row, you can use the following code: |
| 142 | + |
| 143 | +```python |
| 144 | +print("-" * 20) |
| 145 | +``` |
| 146 | +</details> |
| 147 | + |
| 148 | +### Discovery 2: Transforming text |
| 149 | + |
| 150 | +**AI Prompt:** |
| 151 | +> "In Python, how can I change a string input to all uppercase, all lowercase, or title case? Can you show me examples of each?" |
| 152 | +
|
| 153 | +***After the AI responds:** Look closely at the code examples it provides. Try modifying your greeting program to use one of these transformations on the user's name or favorite color. |
| 154 | + |
| 155 | +<details> |
| 156 | +<summary>Show answer</summary> |
| 157 | +You can use the following string methods to transform text: |
| 158 | + |
| 159 | +```python |
| 160 | +name = input("What is your name? ") |
| 161 | + |
| 162 | +# Converts to all uppercase |
| 163 | +uppercase_name = name.upper() |
| 164 | + |
| 165 | +# Converts to all lowercase |
| 166 | +lowercase_name = name.lower() |
| 167 | + |
| 168 | +# Converts to title case (first letter of each word capitalized) |
| 169 | +titlecase_name = name.title() |
| 170 | +``` |
| 171 | +</details> |
| 172 | + |
| 173 | +### Discovery 3: Printing punctuation |
| 174 | + |
| 175 | +> "In Python, what happens if I try to print a string that contains a quote character inside it? How can I do that without causing an error?" |
| 176 | +
|
| 177 | +**After the AI responds:** Pay attention to the different ways it suggests for including quotes in strings. Try each method in the online editor and see how they work. |
| 178 | + |
| 179 | +<details> |
| 180 | +<summary>Show answer</summary> |
| 181 | +You can include quote characters inside a string by using different types of quotes or by escaping them with a backslash. Here are some examples: |
| 182 | + |
| 183 | +```python |
| 184 | +# Using double quotes to include single quotes |
| 185 | +print("It's a beautiful day!") |
| 186 | + |
| 187 | +# Using single quotes to include double quotes |
| 188 | +print('She said, "Hello!"') |
| 189 | + |
| 190 | +# Escaping quotes with a backslash |
| 191 | +print("She said, \"Hello!\"") |
| 192 | +``` |
0 commit comments