Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean indentation in 16-writing-functions #578

Merged
merged 1 commit into from
Dec 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 47 additions & 47 deletions _episodes/16-writing-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,16 +360,16 @@ result of call is: None
> mass = 70 + 20.0 * (2.0 * random.random() - 1.0)
>
> print(mass)
>
>
> # egg sizing machinery prints a label
> if mass >= 85:
> print("jumbo")
> print("jumbo")
> elif mass >= 70:
> print("large")
> print("large")
> elif mass < 70 and mass >= 55:
> print("medium")
> print("medium")
> else:
> print("small")
> print("small")
> ~~~
> {: .language-python}
>
Expand Down Expand Up @@ -433,24 +433,24 @@ result of call is: None
> 1. Complete the statements below to obtain the average GDP for Japan
> across the years reported for the 1980s.
>
> ~~~
> year = 1983
> gdp_decade = 'gdpPercap_' + str(year // ____)
> avg = (japan.loc[gdp_decade + ___] + japan.loc[gdp_decade + ___]) / 2
> ~~~
> {: .language-python}
> ~~~
> year = 1983
> gdp_decade = 'gdpPercap_' + str(year // ____)
> avg = (japan.loc[gdp_decade + ___] + japan.loc[gdp_decade + ___]) / 2
> ~~~
> {: .language-python}
>
> 2. Abstract the code above into a single function.
>
> ~~~
> def avg_gdp_in_decade(country, continent, year):
> df = pd.read_csv('data/gapminder_gdp_'+___+'.csv',delimiter=',',index_col=0)
> ____
> ____
> ____
> return avg
> ~~~
> {: .language-python}
> ~~~
> def avg_gdp_in_decade(country, continent, year):
> df = pd.read_csv('data/gapminder_gdp_'+___+'.csv',delimiter=',',index_col=0)
> ____
> ____
> ____
> return avg
> ~~~
> {: .language-python}
>
> 3. How would you generalize this function
> if you did not know beforehand which specific years occurred as columns in the data?
Expand All @@ -462,41 +462,41 @@ result of call is: None
> >
> > 1. The average GDP for Japan across the years reported for the 1980s is computed with:
> >
> > ~~~
> > year = 1983
> > gdp_decade = 'gdpPercap_' + str(year // 10)
> > avg = (japan.loc[gdp_decade + '2'] + japan.loc[gdp_decade + '7']) / 2
> > ~~~
> > {: .language-python}
> > ~~~
> > year = 1983
> > gdp_decade = 'gdpPercap_' + str(year // 10)
> > avg = (japan.loc[gdp_decade + '2'] + japan.loc[gdp_decade + '7']) / 2
> > ~~~
> > {: .language-python}
> >
> > 2. That code as a function is:
> >
> > ~~~
> > def avg_gdp_in_decade(country, continent, year):
> > df = pd.read_csv('data/gapminder_gdp_' + continent + '.csv', index_col=0)
> > c = df.loc[country]
> > gdp_decade = 'gdpPercap_' + str(year // 10)
> > avg = (c.loc[gdp_decade + '2'] + c.loc[gdp_decade + '7'])/2
> > return avg
> > ~~~
> > {: .language-python}
> > ~~~
> > def avg_gdp_in_decade(country, continent, year):
> > df = pd.read_csv('data/gapminder_gdp_' + continent + '.csv', index_col=0)
> > c = df.loc[country]
> > gdp_decade = 'gdpPercap_' + str(year // 10)
> > avg = (c.loc[gdp_decade + '2'] + c.loc[gdp_decade + '7'])/2
> > return avg
> > ~~~
> > {: .language-python}
> >
> > 3. To obtain the average for the relevant years, we need to loop over them:
> >
> > ~~~
> > def avg_gdp_in_decade(country, continent, year):
> > df = pd.read_csv('data/gapminder_gdp_' + continent + '.csv', index_col=0)
> > c = df.loc[country]
> > gdp_decade = 'gdpPercap_' + str(year // 10)
> > total = 0.0
> > num_years = 0
> > for yr_header in c.index: # c's index contains reported years
> > if yr_header.startswith(gdp_decade):
> > total = total + c.loc[yr_header]
> > num_years = num_years + 1
> > return total/num_years
> > ~~~
> > {: .language-python}
> > df = pd.read_csv('data/gapminder_gdp_' + continent + '.csv', index_col=0)
> > c = df.loc[country]
> > gdp_decade = 'gdpPercap_' + str(year // 10)
> > total = 0.0
> > num_years = 0
> > for yr_header in c.index: # c's index contains reported years
> > if yr_header.startswith(gdp_decade):
> > total = total + c.loc[yr_header]
> > num_years = num_years + 1
> > return total/num_years
> > ~~~
> > {: .language-python}
> >
> > The function can now be called by:
> >
Expand Down