Practice - Creating your own functions
The following assignments are written in a very different format from the previous weeks of python assignments. In each of these assignments you are to write a single function which MUST follow the naming and parameter requirements of the problem exactly. Your file should contain ONLY that function. It should not include any additional code.
When testing your code you should use the shell prompt examples provided. Your code must behave exactly the same way or it will not pass the Autolab tests.
THIS MEANS:
- No input statements in your functions
- No print statements in your functions
- Functions must RETURN the data requested in the problem specification
To complete this activity, you will need to create the following programs:
- FUNC_01 time.py / arrivalEstimate()
- FUNC_02 bmi.py / calculate()
- FUNC_03 median.py / getMedian()
- FUNC_04 leap.py / isLeapYear()
FUNC_01
In a file called time.py create a function called arrivalEstimate()
- This function works very similarly to the code you wrote in 6.e
- In a file called time.py
- Write a function called arrivalEstimate()
- This function takes in two (2) parameters:
- Speed you are moving (assumed to be in MPH)
- Distance you have to travel (assumed to be in miles)
- This function will calculate and return
- An integer
- This number represents the time, in minutes, that it will take you to arrive at your destination.
- This time should be returned rounded to the nearest minute and should not include any decimal value.
- Here is an example:
- Here is another example:
- Here is a third example:
FUNC_02
In a file called bmi.py create a function called calculate()
- In a file called bmi.py
- Write a function called calculate()
- This function takes in three (2) parameters:
- Your weight in pounds
- Your height in feet
- Your height in remaining inches
- The function should return
- A number
- This should be the non-rounded calculation of your BMI
- Recall that the formula for bmi is:
- Here is an example. I am 5 foot 4 and weight 165 lbs:
- Here is another example:
- Here is a third example:
FUNC_03
In a file called median.py create a function called getMedian()
- In a file called median.py
- Write a function called getMedian()
- This function takes in three (3) parameters which can be numbers of any type.
- It is assumed that these numbers have unique values
- In other words
- getMedian(1,2,3) is fine
- getMeidan(1,2,2) is not
- This function will identify and return
- a number
- this number represents the median (the middle) of the three values
- Here are three examples:
- It is worth noting that there are SIX possible combinations:
- first,second,third
- third,second,first
- second,first,third
- second,third,first
- third,first,second
- first,third,second
- You should verify that you are detecting all six combinations.
FUNC_04
In a file called leap.py create a function called isLeapYear()
- This function works very similarly to the code you wrote in 8.d
- In a file called leap.py
- Write a function called isLeapYear()
- This function takes in one (1) parameter:
- A year as a number
- This function will calculate and return
- A boolean value (True or False)
- This value represents whether the given year is a leap year or not
- Here are four examples
Testing Your Code
Note - These assignments are not evaluated for a grade. HOWEVER, they are vitally important to the process of learning how to program. I provided these assignments and the autograders so that you can judge whether you are making progress and so that we can focus our in class conversations in the sessions to come. Please make every effort to complete these problems prior to your next class session.
- Go to the AutoLab Homepage for the course
- https://hypnos.cs.uni.edu/courses/CSED1320-F21/assessments
- Log in if necessary
- For each of the assignments listed above, find their submission link under the "Practice" heading
- Upload the corresponding python file for that assignment.
- When the "handin history" page comes up you will need to refresh the page until a score of either 0 or 1 is shown with your assignment.
- Click on the 0/1 score to get feedback.
- Read the information carefully if you received a score of 0 and figure out what you need to do to fix the problem.
- Ask for help from me or other students if you don't understand an error message.
- Resubmit until the program earns a 1.