Python 1: Introduction to Python
Homework 9
Due
Sunday
Deliverables
There is one deliverable for this assignment
Specification
Write a code that defines these 4 functions
- list_average
- list_minimum
- list_maximum
- above_average
list_average
This functions must have the following header
def list_average(number_list):
This function should compute the average of the
numbers in the list and return that number
rounded to the nearest whole number.
list_minimum
This functions must have the following header
def list_minimum(number_list):
This function should loop through the list
and find the smallest number in the list.
You should assume that no number in the list is
greater than 100.
Use the following algorithm
set a variable to hold the current minimum value to the highest possible value
for each entry in the list
if the entry is smaller than the current minimum
set the current minimum to the entry
list_maximum
This functions must have the following header
def list_maximum(number_list):
This function should loop through the list
and find the largest number in the list.
You should assume that no number in the list is
less than 0.
Use an algorithm similar to the one above.
above_average
This functions must have the following header
def above_average(number_list):
This function should call list_average
to compute the average and count the number of entries
that are greater than that average.
Suggestions
- Create the script file and make it executable.
Run the script.
It should produce no output.
Fix any errors you find.
- Create a function header for each of the four functions.
For the body of each function write pass
.
Copy the test code below to the bottom of your script.
Run the script and fix any errors you find.
- Remove the
pass
statement in list_average.
Set the variable total to 0.
Write a for
loop that loops through the parameter.
Inside the loop, add the value each number to total.
Return total.
Run the script and fix any errors you find.
- Using the value of total and the length of the parameter
(it is a list), change the
return
statement so it returns the average
of the numbers in the list.
Run the script and fix any errors you find.
- Modify the
return
statement so it does not return a decimal.
Run the script and fix any errors you find.
- Remove the
pass
statement in list_minimum.
Set the variable minimum to 100.
Return minimum.
Run the script and fix any errors you find.
- Just before the
return
statement in
list_minimum write a for
loop for every value in the parameter.
Inside the loop write an if
statement that checks
whether the value of each number is less than minimum.
If the number is less, set minimum to this new value.
Run the script and fix any errors you find.
- Remove the
pass
statement in list_maximum.
Set the variable maximum to 0.
Return maximum.
Run the script and fix any errors you find.
- Just before the
return
statement in
list_maximum write a for
loop for every value in the parameter.
Inside the loop write an if
statement that checks
whether the value of each number is greater than maximum.
If the number is greater, set maximum to this new value.
Run the script and fix any errors you find.
- Remove the
pass
statement in above_average.
Call the function list_average using the parameter as the
argument, and store the returned value in the variable average.
Return average.
Run the script and fix any errors you find.
- Before the
return
statement in above_average
set the variable above_count to 0.
Return the value of above_count.
Run the script and fix any errors you find.
- Just before the
return
statement in
above_average write a for
loop for every value in the parameter.
Inside the loop write an if
statement that checks
whether the value of each number is greater than average.
If it is, increment above_count.
Run the script and fix any errors you find.
Test Code
At the bottom of the code you must have the following code
numbers = [62, 60, 58, 50, 85, 93, 99, 77, 72, 74, 61, 68, 73, 65, 57]
print("numbers :", numbers)
print("average :", list_average(numbers))
print("minimum :", list_minimum(numbers))
print("maximum :", list_maximum(numbers))
print("above average:", above_average(numbers))
Testing
When you run the code, your output should look like this
numbers : [62, 60, 58, 50, 85, 93, 99, 77, 72, 74, 61, 68, 73, 65, 57]
average : 70
minimum : 50
maximum : 99
above average: 7