Skip to content

Commit 920a829

Browse files
chennesyjt14den
authored andcommitted
Update writing-functions.md
Add new exercises for writing functions episode.
1 parent 7352f3d commit 920a829

1 file changed

Lines changed: 149 additions & 0 deletions

File tree

episodes/writing-functions.md

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,155 @@ def fahr_to_celsius(temp):
203203
```
204204

205205

206+
::::::::::::::::::::::::::::::::::::::: challenge
207+
208+
## Create a function
209+
210+
Write a function called `addition` that takes two parameters and returns their sum. After defining the function, call it with several arguments and print out the results.
211+
212+
213+
::::::::::::::: solution
214+
215+
## Solution
216+
217+
```python
218+
def addition(x, y):
219+
return x + y
220+
221+
addition(3, 6)
222+
```
223+
224+
```output
225+
9
226+
227+
```
228+
229+
:::::::::::::::::::::::::
230+
231+
::::::::::::::::::::::::::::::::::::::::::::::::::
232+
233+
::::::::::::::::::::::::::::::::::::::: challenge
234+
235+
## Conditional statements within functions
236+
237+
Create a function called `grade_converter` that takes a numerical score (0 - 100) as its parameter and returns a letter grade based on the score:
238+
239+
- 90 and above returns 'A'
240+
- 80 to 89 returns 'B'
241+
- 70 to 79 returns 'C'
242+
- 60 to 69 returns 'D'
243+
- Below 60 returns 'F'
244+
245+
After defining the function, test it with a variety of scores to test it out.
246+
247+
::::::::::::::: solution
248+
249+
## Solution
250+
251+
```python
252+
def grade_converter(score):
253+
if score > 100 or score < 0:
254+
return 'Invalid score'
255+
elif score >= 90:
256+
return 'A'
257+
elif score >= 80:
258+
return 'B'
259+
elif score >= 70:
260+
return 'C'
261+
elif score >= 60:
262+
return 'D'
263+
elif score <= 59:
264+
return 'F'
265+
266+
grade_converter(88)
267+
```
268+
269+
```output
270+
'B'
271+
```
272+
273+
:::::::::::::::::::::::::
274+
275+
::::::::::::::::::::::::::::::::::::::::::::::::::
276+
277+
278+
::::::::::::::::::::::::::::::::::::::: challenge
279+
280+
## Local and global variables
281+
282+
List all of the global variables and all of the local variables in the following code.
283+
284+
```python
285+
fine_rate = 0.25
286+
287+
def fine(days_overdue):
288+
if days_overdue <= 10:
289+
fine = days_overdue * fine_rate
290+
else:
291+
fine = (days_overdue * fine_rate) + (days_overdue * (fine_rate*2))
292+
return fine
293+
294+
total_fine = calc_fine(20)
295+
f'Fine owed: ${total_fine:.2f}'
296+
297+
```
298+
299+
```output
300+
'Fine owed: $15.00'
301+
```
302+
::::::::::::::: solution
303+
304+
## Solution
305+
306+
Global variables:
307+
308+
- fine_rate
309+
- total_fine
310+
311+
Local variables:
312+
313+
- days_overdue
314+
- fine
315+
316+
:::::::::::::::::::::::::
317+
318+
::::::::::::::::::::::::::::::::::::::::::::::::::
319+
320+
::::::::::::::::::::::::::::::::::::::: challenge
321+
322+
## CSVs to Pandas function
323+
In the [Looping Data Sets episode](looping-data-sets.html#appending-dataframes-to-a-list), we learned to use glob to loop through a directory of CSV files and convert them to a Pandas DataFrame.
324+
325+
Write a function that converts a directory of CSV files into a single Pandas DataFrame. The function should accept one parameter: a string that includes the path and glob wildcard expression to point to a set of CSV files (e.g., `'data/*.csv'`). We can assume, for these purposes, that all of the DataFrames have the same column names so that you can use `pd.concat(dfs, ignore_index=True)` at the end of the function to concatenate a list of DataFrames into a single DataFrame.
326+
327+
328+
::::::::::::::: solution
329+
330+
## Solution
331+
332+
```python
333+
import glob
334+
import pandas as pd
335+
336+
def concat_csvs(path):
337+
338+
dfs = []
339+
340+
for csv in sorted(glob.glob(path)):
341+
data = pd.read_csv(csv)
342+
dfs.append(data)
343+
344+
df = pd.concat(dfs, ignore_index=True)
345+
return df
346+
347+
df = concat_csvs('data/*.csv')
348+
349+
```
350+
351+
:::::::::::::::::::::::::
352+
353+
::::::::::::::::::::::::::::::::::::::::::::::::::
354+
206355
:::::::::::::::::::::::::::::::::::::::: keypoints
207356

208357
- Break programs down into functions to make them easier to understand.

0 commit comments

Comments
 (0)