关键词 > Python代写

Project 2: Using Functions, Writing Functions, and Conditionals

发布时间:2021-07-04

Project 2: Using Functions, Writing Functions, and Conditionals


For this project, start a new Jupyter notebook named p2.ipynb. In the first cell of your notebook, paste this, then modify fill in your NETID and an estimate of how long you spent on the project:

# project: p2

# submitter: NETID

# partner: none

# hours: ????

In this project, we’ll explore how much the pandemic affected how often people in Madison biked during 2020. We’ll provide a simplified table, based on these datasets:

https://data-

   cityofmadison.opendata.arcgis.com/datasets/8860784eb30e4a45a6f853b5f8 

   1949f2/explore

https://data-

   cityofmadison.opendata.arcgis.com/datasets/367cb53685c74628b4975d8f65

   d20748/explore

We have summarized the above data to give per-month totals at two different bike counters, placed on the Capital City Trail and Southwest Path:

The above table is in a file called covid-bikes.csv – download that to the directory that contains the notebook you created.

This semester, you’ll eventually learn how to write your own code to access CSV tables. For now, download cs220.py to wherever your notebook is. This as a Python module we wrote to help you access values (cells) in the table.

Try running:

import cs220

cs220.load_table("covid-bikes.csv")

cs220.cell("April 2018", "capital")

The two values passed to the cell function determine the row and column, so you should get '30122'. Note that even though we’ll want numbers for most calculations, you’ll need to deal with the fact that cell returns a string.

If you don’t want to use cs220. in front of every function call, you can use a from ____ import ____ import style:

from cs220 import load_table, cell

load_table("covid-bikes.csv")

cell("April 2018", "capital")


Testing

Download the expected answers and tester.py to your project directory. Run tester.py p2.ipynb in the terminal to check whether your answers are considered close enough to the expected answers.


Group Part (75%)

Using Functions

Q1: how many people rode the Capital City trail in August of 2020?

Note that this value is missing in the table, so that will be a fine answer.

Q2: how many people rode the Capital City trail in August of 2019?

This one has an actual number, so your answer should be an int.

Q3: what is the average January ridership (over the three years) on the Southwest trail?

Q4: how many people rode the Capital City trail in all of 2018?

Let’s plan for somebody later asking a similar question for a different trail or year. We should write our code so that such changes to the question require minimal changes to the code. A good strategy here is to create two variables named the following:

path

year

Then we can complete the following to include November and December in the count:

total = (

int(cell("January " + str(year), path)) +

int(cell("February " + str(year), path)) +

int(cell("March " + str(year), path)) +

int(cell("April " + str(year), path)) +

int(cell("May " + str(year), path)) +

int(cell("June " + str(year), path)) +

int(cell("July " + str(year), path)) +

int(cell("August " + str(year), path)) +

int(cell("September " + str(year), path)) +

int(cell("October " + str(year), path))

)


Writing Functions

Requirement: lookup function

Write a function named lookup that has three parameters:

● path

● month

● year

It should return the bike ridership for the specified time/location as an int. If no year is specified, the year should default to 2020.

Q5: what is lookup("southwest", "January", 2019)?

Q6: what is lookup("capital", "January")?

Requirement: year_sum function

Write a function named year_sum that has two parameters:

● path

● year

Hint: reuse the code from question 4. Now, instead of being regular variables, path and year are parameters (which are a special kind of variable).

Q7: what is year_sum("capital", 2019)

Q8: what is year_sum("southwest", 2019)

Requirement: is_missing function

Write a function named is_missing that has three parameters:

path

● month

● year

Return True if the data is missing; otherwise, return False.

Requirement: the function should contain one line of code (no if statement).

Q9: what is is_missing("capital", "July", 2020)?

Q10: what is is_missing("southwest", "July", 2020)?

Requirment: covid_impact function

Write a function named covid_impact that has two parameters:

● path

● month

Compare the given month in 2020 to the given month in 2019. Return a string something like this:

"riders up 10%" or "riders down 2%"

Use the round function.

Q11: what is covid_impact("southwest", "May")

Q12: what is covid_impact("capital", "May")

Q13: what is covid_impact("capital", "June")


Conditionals

Requirement: save_month_avg function

Write a function named save_month_avg that has 2 parameters:

● path

● month

It should return the average for that month over the three years in the dataset. It should use the is_missing function. If some data is missing, average over whatever is there.

For example, if you run this:

print(cell("July 2018", "capital"),

        cell("July 2019", "capital"),

        cell("July 2020", "capital"))

You should get this: 90426 87847 missing

In this case, the average should be over 90426 and 87847 only.

Q14: what is save_month_avg("capital", "July")

Q15: what is save_month_avg("capital", "June")


Individual Part (25%)

Requirement: numeric month for lookup function

Modify your lookup function from earlier so that lookup("southwest", 1, 2019) is a shorthand for lookup("southwest", "January", 2019). 1 is January, 2 is February, etc.

Do NOT copy the function – modify the earlier code (as a general rule, you should never have multiple functions with the same name in the same notebook).

Q16: what is lookup("capital", 1, 2019)?

Requirement: get_next_sw function

This function will use some global variables, so create those first:

next_sw_year = 2018

next_sw_month = 11

Every time get_next_sw is called, it returns the number of riders for a particular month on the Southwest trail, starting in November of 2018 and moving forward in time. The function doesn’t take any arguments.

The first time it is called, it should return the riders in November of 2018 (based on the two global variables we just created); it should also increase next_sw_month by one. The second time it is called, it should return riders for Decemember of 2018 (since next_sw_month is now 12). As there are only 12 months, and next_sw_month is already 12, it should reset next_sw_month to 1 and increase next_sw_year instead. Thus, the third time is it called, it will return the riders for January of 2019.

Q17: what does get_next_sw() return the 1st time it is called?

Q18: what does get_next_sw() return the 2nd time it is called?

Q19: what does get_next_sw() return the 3rd time it is called?

Q20: what does get_next_sw() return the 4th time it is called?