07 Team Activity: Lists
Instructions
Work as a team as explained in the instructions for the lesson 2 team activity.
Purpose
There are a few details about writing and calling functions in Python that, if you understand, will help you be a more effective programmer. These details include default parameter values and pass by reference. As a team during this activity, you will write and call a function that demonstrates both default parameter values and pass by reference.
Helpful Documentation
- The Python programming language allows a programmer to specify a default value for a function parameter. When a parameter has a default value, the corresponding argument is optional. The preparation content for lesson 4 includes a section about default parameter values.
Within a Python program, when a number is passed as an argument to a function, the computer copies the number from the argument into the parameter. In other words, the parameter gets a copy of the value that is in the argument. Copying the value of an argument into a parameter is known as pass by value.
In Python, when a list is passed as an argument to a function, the computer does not copy the list. Instead, the computer copies a reference to the list into the parameter. This means the argument and parameter refer to the same list. Copying a reference from an argument into a parameter is known as pass by reference. With pass by reference, if a called function changes a list that was passed to the function, this will of course change the list in both the calling and called functions because both the argument and parameter refer to the same list.
The preparation content for this lesson includes a section about passing arguments by value and by reference.
- The Python
random
module contains functions to generate pseudo random numbers. Therandom.uniform
function generates random floating-point numbers. - The Python
random
module contains a function namedchoice
that randomly chooses one element from a list. - The built-in Python
round
function rounds a number to a specified number of digits after the decimal place.
Assignment
As a team, write a Python program named
random_numbers.py
that creates a list of numbers,
appends more numbers onto the list, and prints the list. The program
must have two functions named main
and
append_random_numbers
as follows:
main
- Has no parameters
- Creates a list named numbers like this:
numbers = [16.2, 75.1, 52.3]
- Prints the numbers list
- Calls the
append_random_numbers
function with only one argument to add one number to numbers - Prints the numbers list
- Calls the
append_random_numbers
function again with two arguments to add three numbers to numbers - Prints the numbers list
append_random_numbers
- Has two parameters: a list named numbers_list and an integer named quantity. The parameter quantity has a default value of 1
- Computes quantity pseudo random numbers by calling the
random.uniform
function. - Rounds the quantity pseudo random numbers to one digit after the decimal.
- Appends the quantity pseudo random numbers onto the end of the numbers_list.
At the bottom of your program, write an if statement
that calls the main
function. Then run your program and
verify that your program works correctly.
Core Requirements
- Your program includes two functions named
main
andappend_random_numbers
. Theappend_random_numbers
function has two parameters named numbers_list and quantity, and quantity has a default value of 1. - The
main
function callsappend_random_numbers
twice, first with one argument and second with two arguments. - The append_random_numbers function includes a loop that appends quantity random numbers at the end of numbers_list.
Stretch Challenges
If your team finishes the core requirements in less than an hour, complete one or more of these stretch challenges. Note that the stretch challenges are optional.
- Add a function named
append_random_words
that meets the following criteria:- Has two parameters: a list named words_list and an integer named quantity. The parameter quantity has a default value of 1
- Randomly selects quantity words from a list of words and appends the selected words at the end of words_list.
- Add statements in the
main
function that create a list of words, call theappend_random_words
function, and then print the list of words. - Add something or change something in your program that you think would make your program better, easier for the user, more elegant, or more fun. Be creative.
Testing Procedure
Verify that your program works correctly by following each step in this testing procedure:
- Download the
test_random_numbers.py
Python file and save it in the same folder where you saved yourrandom_numbers.py
program. Run thetest_random_numbers.py
file and ensure that thetest_random_numbers
function passes. If it doesn’t pass, there is a mistake in yourrandom_numbers
function. Read the output frompytest
, fix the mistake, and runtest_random_numbers.py
again until the test function passes.> python test_random_numbers.py =================== test session starts ==================== platform win32--Python 3.8.6, pytest-6.1.2, py-1.9.0, pluggy rootdir: C:\Users\cse111\lesson07 collected 1 item test_random_numbers.py::test_random_numbers PASSED [100%] ==================== 1 passed in 0.11s =====================
- Run your program and ensure that your program’s output is similar* to the output below.
> python random_numbers.py numbers [16.2, 75.1, 52.3] numbers [16.2, 75.1, 52.3, 84.2] numbers [16.2, 75.1, 52.3, 84.2, 99.5, 20.4, 25.3] words ['join', 'love', 'smile', 'love', 'cloud', 'head']
- Because your program is appending random numbers to the numbers list, your program’s output will be slightly different than the output shown above. Specifically, the last four random numbers and the random words will be different.
Sample Solution
Please work diligently with your team for the one hour meeting. After the meeting is over, please compare your solution to the sample solution. Please do not look at the sample solution until you have either finished the program or diligently worked for at least one hour. At the end of the hour, if you are still struggling to complete the assignment, you may use the sample solution to help you finish.
Ponder
Look again at the append_random_numbers
function
(and the append_random_words
function if you wrote it
for the stretch challenge). The first parameter,
numbers_list, is a list. Recall that in Python, lists are
passed by reference which means that any changes made to the list in
the append_random_numbers
(and
append_random_words
) function will change the list in
main
. You can verify this is true by looking at the
output of your program. The numbers that were appended onto the list
by the append_random_numbers
function are stored in the
same list in main
.
Submission
When you have finished the activity, please report your progress via the associated I‑Learn quiz. When asked about which of the requirements you completed, feel free to include any work done during the team meeting or after the meeting, including work done with the help of the sample solution, if necessary. In short, report on what you were able to accomplish, regardless of when you completed it or if you needed help from the sample solution.