|
4 | 4 | "metadata": {}, |
5 | 5 | "cell_type": "markdown", |
6 | 6 | "source": [ |
7 | | - "# Packages and File Handling\n", |
| 7 | + "# Packages, File Handling, and User Input\n", |
8 | 8 | "\n", |
9 | 9 | "> _Why bother reinventing the wheel when someone else has already done the work for you? ~ unknown, c. 2019, in conversation_\n", |
10 | 10 | "\n", |
|
90 | 90 | ], |
91 | 91 | "execution_count": 18 |
92 | 92 | }, |
| 93 | + { |
| 94 | + "metadata": {}, |
| 95 | + "cell_type": "markdown", |
| 96 | + "source": [ |
| 97 | + "## user input\n", |
| 98 | + "\n", |
| 99 | + "OKAY so like a huge part of designing programs that people uh actually use is user input. im lowk not sure why i didnt put this earlier but you kinda gotta know some other basics before you can really use it well. Also, there just wasn't room.\n", |
| 100 | + "\n", |
| 101 | + "User input in desktop applications will be covered in a later lesson, however we'll be covering terminal user input now.\n", |
| 102 | + "\n", |
| 103 | + "User input in python is handled with the `input()` function. What the user submits will be stored in a variable, if you choose to handle the output." |
| 104 | + ], |
| 105 | + "id": "1386a34ef7e2caa5" |
| 106 | + }, |
| 107 | + { |
| 108 | + "metadata": { |
| 109 | + "ExecuteTime": { |
| 110 | + "end_time": "2025-08-26T20:46:16.343963Z", |
| 111 | + "start_time": "2025-08-26T20:46:08.795884Z" |
| 112 | + } |
| 113 | + }, |
| 114 | + "cell_type": "code", |
| 115 | + "source": [ |
| 116 | + "print(\"What's your favorite ice cream flavor?\")\n", |
| 117 | + "flavor = input()\n", |
| 118 | + "print(flavor + \"? That's a good one\") # blatant lie" |
| 119 | + ], |
| 120 | + "id": "b4012e73454f47df", |
| 121 | + "outputs": [ |
| 122 | + { |
| 123 | + "name": "stdout", |
| 124 | + "output_type": "stream", |
| 125 | + "text": [ |
| 126 | + "What's your favorite ice cream flavor?\n", |
| 127 | + "chocolate? That's a good one\n" |
| 128 | + ] |
| 129 | + } |
| 130 | + ], |
| 131 | + "execution_count": 2 |
| 132 | + }, |
| 133 | + { |
| 134 | + "metadata": {}, |
| 135 | + "cell_type": "markdown", |
| 136 | + "source": [ |
| 137 | + "However, it is possible to prompt the user with the input function in order to give them a question instead of just a blank line. The input function accepts parameters so you can directly prompt the user.\n", |
| 138 | + "You can also just do a blank `input()` and disregard the output as a \"Press enter to continue\" thing." |
| 139 | + ], |
| 140 | + "id": "3d5fd8e4d073a927" |
| 141 | + }, |
| 142 | + { |
| 143 | + "metadata": { |
| 144 | + "ExecuteTime": { |
| 145 | + "end_time": "2025-08-26T20:58:26.554073Z", |
| 146 | + "start_time": "2025-08-26T20:58:21.866381Z" |
| 147 | + } |
| 148 | + }, |
| 149 | + "cell_type": "code", |
| 150 | + "source": [ |
| 151 | + "pet = input(\"Do you have any pets?\")\n", |
| 152 | + "print(\"You have a \" + pet + \"? Omg so do i!! i have 500 \" + pet + \"s!\") # me when i lie" |
| 153 | + ], |
| 154 | + "id": "c0e9188ae8a2c0c3", |
| 155 | + "outputs": [ |
| 156 | + { |
| 157 | + "name": "stdout", |
| 158 | + "output_type": "stream", |
| 159 | + "text": [ |
| 160 | + "You have a no? Omg so do i!! i have 500 nos!\n" |
| 161 | + ] |
| 162 | + } |
| 163 | + ], |
| 164 | + "execution_count": 3 |
| 165 | + }, |
| 166 | + { |
| 167 | + "metadata": {}, |
| 168 | + "cell_type": "markdown", |
| 169 | + "source": [ |
| 170 | + "if you think about it, file handling is just like user input from the computer (right?) kinda... not really...\n", |
| 171 | + "But you could even chain the two together to have the program read from a file of the user's choice, essentially uploading the file to your python program. with out further adieu, let us transition (heh) to file handling!" |
| 172 | + ], |
| 173 | + "id": "105879e1945829e3" |
| 174 | + }, |
93 | 175 | { |
94 | 176 | "metadata": {}, |
95 | 177 | "cell_type": "markdown", |
|
132 | 214 | "\n", |
133 | 215 | "There are several other modes you can use when opening a file:\n", |
134 | 216 | "- `\"r\"`: read mode (default). This mode is used to read data from a file. If the file doesn't exist, an error will be raised. Good for reading in data.\n", |
135 | | - "- `\"a\"`: append mode. This mode is used to add data to the end of a file. If the file doesn't exist, it will be created. Good for adding data to a file without overwriting existing data, like a log file or user data.\n", |
136 | | - "-" |
| 217 | + "- `\"a\"`: append mode. This mode is used to add data to the end of a file. If the file doesn't exist, it will be created. Good for adding data to a file without overwriting existing data, like a log file or user data." |
137 | 218 | ], |
138 | 219 | "id": "670fa24c371c3fbb" |
| 220 | + }, |
| 221 | + { |
| 222 | + "metadata": {}, |
| 223 | + "cell_type": "code", |
| 224 | + "outputs": [], |
| 225 | + "execution_count": null, |
| 226 | + "source": [ |
| 227 | + "with open(\"example.txt\", \"a\") as f:\n", |
| 228 | + " f.write(\"I'm appending this!\")" |
| 229 | + ], |
| 230 | + "id": "3cb3750872150b82" |
| 231 | + }, |
| 232 | + { |
| 233 | + "metadata": {}, |
| 234 | + "cell_type": "code", |
| 235 | + "outputs": [], |
| 236 | + "execution_count": null, |
| 237 | + "source": [ |
| 238 | + "with open(\"example.txt\", \"r\") as f:\n", |
| 239 | + " content = f.read()\n", |
| 240 | + " print(content)" |
| 241 | + ], |
| 242 | + "id": "30fec8d8afca341f" |
| 243 | + }, |
| 244 | + { |
| 245 | + "metadata": {}, |
| 246 | + "cell_type": "markdown", |
| 247 | + "source": [ |
| 248 | + "### practice exercise: reading a user selected file\n", |
| 249 | + "\n", |
| 250 | + "write code that will prompt the user for a filename, and open that file. then, prompt the user for some new content, and write that to a different user-selected file" |
| 251 | + ], |
| 252 | + "id": "ed4b14a7b0a35350" |
| 253 | + }, |
| 254 | + { |
| 255 | + "metadata": {}, |
| 256 | + "cell_type": "code", |
| 257 | + "outputs": [], |
| 258 | + "execution_count": null, |
| 259 | + "source": [ |
| 260 | + "# write your code here!\n", |
| 261 | + "# you can use example.txt for practice." |
| 262 | + ], |
| 263 | + "id": "a03d6b6716f8ded8" |
139 | 264 | } |
140 | 265 | ], |
141 | 266 | "metadata": { |
|
0 commit comments