W04 Code-along Activity: Lists
Code-along Activity Overview
For this activity, you will code along with an instructor video that walks you through each step of the activity below. Make sure that you pause the video, or watch sections over if you need to, so that you are able to complete the entire activity.
Your instructor will post a link to the video in Microsoft Teams.
Background
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.
Program
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:
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.
Enhancements
Here is a list of enhancements that you could make to the program. Your instructor will walk you through at least one of them. Feel free to complete others.
-
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.
Instructions
- Create a folder for this week's code-along program.
- Open the folder you just created in VSCode.
- Create a new file named random_numbers.py.
- Find the video that your instructor has posted for this assignment and code along with them to complete each of the requirements. Work through the requirements in order with the instructor rather than jumping ahead to more complicated steps to ensure you do not miss any concepts.
Alternative Video Link and Transcript
In addition to the link that your instructor posted, you are also welcome to watch the following code walkthrough or use its transcript:
Additional Resources
You may use these additional resources for further study.
Sample Solution
In addition to the instructor video, you can also view this solution. (This solution may be slightly different than the final code that you and your instructor created.)
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 week 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.
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\week04 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.
Submission
Please pause and re-watch any sections of the instructor video necessary until you have completed the entire activity and your program runs the same as the instructor's. When you are finished:
- Return to Canvas to take the quiz.
Useful Links:
- Return to: Week Overview | Course Home