Skip to content

Commit 169fe0d

Browse files
finish up file handling and user input
1 parent c4d5176 commit 169fe0d

File tree

1 file changed

+128
-3
lines changed

1 file changed

+128
-3
lines changed

File_Handling_and_Packages.ipynb

Lines changed: 128 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"metadata": {},
55
"cell_type": "markdown",
66
"source": [
7-
"# Packages and File Handling\n",
7+
"# Packages, File Handling, and User Input\n",
88
"\n",
99
"> _Why bother reinventing the wheel when someone else has already done the work for you? ~ unknown, c. 2019, in conversation_\n",
1010
"\n",
@@ -90,6 +90,88 @@
9090
],
9191
"execution_count": 18
9292
},
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+
},
93175
{
94176
"metadata": {},
95177
"cell_type": "markdown",
@@ -132,10 +214,53 @@
132214
"\n",
133215
"There are several other modes you can use when opening a file:\n",
134216
"- `\"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."
137218
],
138219
"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"
139264
}
140265
],
141266
"metadata": {

0 commit comments

Comments
 (0)