|
| 1 | +# Just Enough Python: LLM Examples |
| 2 | + |
| 3 | +This document has example programs to give LLMs inspiration when helping |
| 4 | +learners study Just Enough Python. |
| 5 | + |
| 6 | +It is helpful to use scaffolding: start with extremely simple programs and |
| 7 | +plenty of explanatory comments, then gradually introduce more complexity and |
| 8 | +less support as the learner progresses. |
| 9 | + |
| 10 | +## Fill in the Blanks |
| 11 | + |
| 12 | +This program demonstrates using `_` in the code as an exercise for learners. You |
| 13 | +can also use letters in the blanks and ask specific questions about each blank. |
| 14 | + |
| 15 | +```Python |
| 16 | +""" |
| 17 | +Fill in the Blanks Exercise |
| 18 | +
|
| 19 | +This program demonstrates using _ in the code as an exercise for learners. |
| 20 | +You can also use letters in the blanks and ask specific questions about each blank. |
| 21 | +""" |
| 22 | + |
| 23 | +# --- gather user phrase --- |
| 24 | + |
| 25 | +phrase = None |
| 26 | +while phrase is None or phrase == "": |
| 27 | + phrase = input('enter a phrase: ') |
| 28 | + phrase = phrase.strip() |
| 29 | + |
| 30 | +# --- get user's choice --- |
| 31 | + |
| 32 | +keep_letters = input( |
| 33 | + '"ok" to remove everything that is not a letter\n' + |
| 34 | + '"cancel" to repeat each character: ' |
| 35 | +).strip() |
| 36 | + |
| 37 | +# --- process the phrase based on choice --- |
| 38 | + |
| 39 | +new_phrase = '' |
| 40 | +if keep_letters == "ok": # This replaces __B__ in the original |
| 41 | + # --- keep only letters --- |
| 42 | + |
| 43 | + letters = 'abcdefghijklmnopqrstuvwxyz' |
| 44 | + for character in phrase: # This replaces __C__ in the original |
| 45 | + if character.lower() in letters: # This replaces __D__ in the original |
| 46 | + new_phrase = new_phrase + character |
| 47 | +else: |
| 48 | + # --- double each character --- |
| 49 | + |
| 50 | + for character in phrase: |
| 51 | + new_phrase = new_phrase + character + character # This replaces __E__ |
| 52 | + |
| 53 | +# --- show the result --- |
| 54 | +print(new_phrase) |
| 55 | + |
| 56 | +""" |
| 57 | +Comprehension questions: |
| 58 | +
|
| 59 | +- Which interaction belongs in A? How can you tell? |
| 60 | +- What happens if the user just hits enter in A? How does the program respond? |
| 61 | +- Which variable belongs in B? What type does it store? How can you tell? |
| 62 | +- Which variable belongs in C? Where does its value come from? |
| 63 | +- Is it possible to know from the source code how many times the program will loop over C? |
| 64 | +- What method belongs in D? Why is the string changed to lowercase before? |
| 65 | +- Which variable belongs in E? What role does this variable have? |
| 66 | +""" |
| 67 | +``` |
| 68 | + |
| 69 | +Comprehension questions: |
| 70 | + |
| 71 | +- Which interaction belongs in **A**? How can you tell? |
| 72 | +- What happens if the user cancels the prompt in **A**? How does the program |
| 73 | + respond? |
| 74 | +- Which variable belongs in **B**? What type does it store? How can you tell? |
| 75 | +- Which variable belongs in **C**? Where does its value come from? |
| 76 | +- Is it possible to know from the source code how many times the program will |
| 77 | + loop over **C**? |
| 78 | +- What method belongs in **D**? Why is the string changed to lowercase before? |
| 79 | +- Which variable belongs in **E**? What role does this variable have? |
| 80 | + |
| 81 | +## Refactoring |
| 82 | + |
| 83 | +```Python |
| 84 | +""" |
| 85 | +Refactoring Exercise |
| 86 | +
|
| 87 | +What strategy can replace the need for continue? |
| 88 | +How might you redesign the loop to avoid this keyword entirely? |
| 89 | +""" |
| 90 | + |
| 91 | +to_be_frogged = None |
| 92 | + |
| 93 | +while to_be_frogged is None or to_be_frogged == "": |
| 94 | + to_be_frogged = input( |
| 95 | + 'enter some text to frogify.\n' + |
| 96 | + '- "f" will be replaced with "frog"\n' + |
| 97 | + '- "F" will be replaced with "FROG": ' |
| 98 | + ).strip() |
| 99 | + |
| 100 | +frogged = '' |
| 101 | + |
| 102 | +for character in to_be_frogged: |
| 103 | + if character == 'f': |
| 104 | + frogged = frogged + 'frog' |
| 105 | + continue |
| 106 | + if character == 'F': |
| 107 | + frogged = frogged + 'FROG' |
| 108 | + continue |
| 109 | + frogged = frogged + character |
| 110 | + |
| 111 | +print(frogged) |
| 112 | +``` |
| 113 | + |
| 114 | +## Modifying |
| 115 | + |
| 116 | +```python |
| 117 | +""" |
| 118 | +Modifying Exercise |
| 119 | +
|
| 120 | +How could you modify this program so it checks that user input is SHORTER than a specific limit? |
| 121 | +""" |
| 122 | + |
| 123 | +limit = 5 |
| 124 | +phrase = '' |
| 125 | +long_enough = False |
| 126 | + |
| 127 | +while not long_enough: |
| 128 | + phrase = input('enter anything longer than ' + str(limit) + ' characters: ') |
| 129 | + |
| 130 | + if phrase == "": |
| 131 | + print('there is no escape') |
| 132 | + elif len(phrase) <= limit: |
| 133 | + print('too short') |
| 134 | + else: |
| 135 | + long_enough = True |
| 136 | + |
| 137 | +print('"' + phrase + '" is ' + str(len(phrase)) + ' characters long') |
| 138 | +``` |
| 139 | + |
| 140 | +## Naming Variables |
| 141 | + |
| 142 | +```Python |
| 143 | +""" |
| 144 | +Naming Variables Exercise |
| 145 | +
|
| 146 | +Fill in appropriate names for variables A, B, and C |
| 147 | +""" |
| 148 | + |
| 149 | +__A__ = None |
| 150 | +while __A__ is None or __A__ == "": |
| 151 | + __A__ = input('enter some text, each character will be repeated: ') |
| 152 | + __A__ = __A__.strip() |
| 153 | + |
| 154 | +__B__ = '' |
| 155 | +for __C__ in __A__: |
| 156 | + __B__ = __B__ + __C__ + __C__ |
| 157 | + |
| 158 | +print(__A__ + ' -> ' + __B__) |
| 159 | + |
| 160 | +""" |
| 161 | +Comprehension questions: |
| 162 | +
|
| 163 | +- What is this program's behavior? |
| 164 | +- What would be a good name for each variable? |
| 165 | +""" |
| 166 | +``` |
| 167 | + |
| 168 | +Comprehension questions: |
| 169 | + |
| 170 | +- What is this program's behavior? |
| 171 | +- What would be a good name for each variable? |
| 172 | + |
| 173 | +## Users Stories + Test Cases + Review Checklist |
| 174 | + |
| 175 | +As learners progress you can also start to discuss user stories, test cases and |
| 176 | +code review checklists. Because the programs are simple it's enough to use |
| 177 | +formatted comments for these - Welcome to JS does not use any libraries for |
| 178 | +testing but does use ESLint. |
| 179 | + |
| 180 | +```Python |
| 181 | +"""Magic Mirror |
| 182 | +
|
| 183 | +A user can input a non-empty string and only the letters will be turned into a mirror |
| 184 | + - given the user hits enter with no input, they will be prompted again |
| 185 | + - given their input is valid, the loop will exit and the mirrored letters will be displayed |
| 186 | +
|
| 187 | +Test cases: |
| 188 | + only letters: |
| 189 | + 'abc' -> 'abc|cba' |
| 190 | + 'hello' -> 'hello|olleh' |
| 191 | + 'JavaScript' -> 'JavaScript|tpircSavaJ' |
| 192 | + only not-letters: |
| 193 | + '.(-).' -> '|' |
| 194 | + '-=>|<=-' -> '|' |
| 195 | + '. - ^ - .' -> '|' |
| 196 | + mixed letters and not-letters: |
| 197 | + 'hello!' -> 'hello|olleh' |
| 198 | + 'good bye?' -> 'goodbye|eybdoog' |
| 199 | + 'let input = ""' -> 'letinput|tupnitel' |
| 200 | +""" |
| 201 | + |
| 202 | +# Get valid input from user |
| 203 | +text = None |
| 204 | +while text is None or text == "": |
| 205 | + text = input('Enter text to mirror: ') |
| 206 | + text = text.strip() |
| 207 | + |
| 208 | +# Extract only letters from the input |
| 209 | +letters = '' |
| 210 | +for char in text: |
| 211 | + if char.lower() in 'abcdefghijklmnopqrstuvwxyz': |
| 212 | + letters = letters + char |
| 213 | + |
| 214 | +# Create the mirrored output |
| 215 | +# First build the reversed string |
| 216 | +reversed_letters = '' |
| 217 | +for i in range(len(letters) - 1, -1, -1): |
| 218 | + reversed_letters = reversed_letters + letters[i] |
| 219 | + |
| 220 | +# Combine original and reversed with separator |
| 221 | +result = letters + '|' + reversed_letters |
| 222 | +print(result) |
| 223 | + |
| 224 | +""" |
| 225 | +Checklist: |
| 226 | + [ ] the code is formatted |
| 227 | + [ ] variable names are clear and helpful |
| 228 | + [ ] each line of code is explained in a comment above that line |
| 229 | + - use full sentences and correct Python vocabulary |
| 230 | + [ ] the program runs |
| 231 | + [ ] the program has no errors |
| 232 | + [ ] all of the test cases work |
| 233 | + [ ] you tested strange inputs that could break your program (edge cases) |
| 234 | +""" |
| 235 | +``` |
0 commit comments