CSE 111: Programming with Functions

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

  1. Your program includes two functions named main and append_random_numbers. The append_random_numbers function has two parameters named numbers_list and quantity, and quantity has a default value of 1.
  2. The main function calls append_random_numbers twice, first with one argument and second with two arguments.
  3. 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.

  1. Add a function named append_random_words that meets the following criteria:
    1. Has two parameters: a list named words_list and an integer named quantity. The parameter quantity has a default value of 1
    2. Randomly selects quantity words from a list of words and appends the selected words at the end of words_list.
  2. Add statements in the main function that create a list of words, call the append_random_words function, and then print the list of words.
  3. 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

  1. Create a folder for this week's code-along program.
  2. Open the folder you just created in VSCode.
  3. Create a new file named random_numbers.py.
  4. 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
Testing Procedure

Verify that your program works correctly by following each step in this testing procedure:

  1. Download the test_random_numbers.py Python file and save it in the same folder where you saved your random_numbers.py program. Run the test_random_numbers.py file and ensure that the test_random_numbers function passes. If it doesn’t pass, there is a mistake in your random_numbers function. Read the output from pytest, fix the mistake, and run test_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 =====================
                
  2. 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:

Useful Links: