Functions

Overview

This week's lab will cover the following:

Lecture Slides

Creating your Github Repo for Lab 7

Use the following link to set up a GitHub repository for Lab 7. Open your Lab 7 repository in GitHub Codespaces, and open the provided template lib.py. Update the comment block with the appropriate information for name, date, and usage.

Note: There is also a lab7a.py & lab7b.py, which you will be using later in the lab.

Functions

Functions are a way to organize your code into reusable blocks. Functions are defined using the def keyword followed by the function name and parentheses. The function name should be descriptive of what the function does. The parentheses can contain arguments that are passed to the function. The function block is indented and contains the code that the function will execute. The function can return a value using the return keyword. Use the following code to create a function called combine that accepts the arguments prefix, suffix & place. It will then return the combined string of all three arguments in the following format: "prefix suffix of place".

		def combine(prefix, suffix, place):
			return prefix + " " + suffix + " of " + place
	

You will be using this function in lab7a.py & lab7b.py.

lab7a.py

Open the provided template lab7a.py. Update the comment block with the appropriate information for name, date, and usage.

Importing Modules and Functions

To do: Import the random module.

So far you have been writing all your code in a single file. As your programs grow in size, it is a good idea to break them up into smaller, more manageable pieces. Python allows you to import functions from other files. This is done using the import keyword followed by the name of the file (without the .py extension). You can then call the function as if it had been written in the current program. Use the following code to import the combine function from the lib file.

		from lib import combine
	

To do: Create the following lists

prefixes

suffixes

places

To do: Prompt the user

Using the prompt "How many items would you like to generate? " store the user's input in the variable numItems.

To do: Generate the items

The following code will use a for loop to generate the number of items the user requested using:

	for item in range(int(numItems)):
		prefix = prefixes[random.randint(0, len(prefixes)-1)]
		suffix = suffixes[random.randint(0, len(suffixes)-1)]
		place = places[random.randint(0, len(places)-1)]
	

Still in the for loop, use the combine function to generate the item name and print it to the screen.

		item = combine(prefix, suffix, place)
		print(item)
	

Run your script and test it with the value 5. It should generate 5 items with random prefixes, suffixes, and places. When you are done testing, run the lab7a_test.py test script to confirm it passes with a 0 exit code. Once it does, commit your changes and push them to your GitHub repository. Your script should produce output similar to the following. If it does not, go back and complete whatever you've missed.

lab7a.py sample output

lab7b.py

Open the provided template lab7b.py. Update the comment block with the appropriate information for name, date, and usage. Using what you learned in lab7a.py, fill in the template with the following:

prefixes

suffixes

places

To do:

Using what you learned in lab7a.py, complete the following:

Run your script and test it with the value 5. It should generate 5 names with random prefixes, suffixes, and places. When you are done testing, run the lab7b_test.py test script to confirm it passes with a 0 exit code. Once it does, commit your changes and push them to your GitHub repository. Your script should produce output similar to the following. If it does not, go back and complete whatever you've missed.

lab7a.py sample output

Completing the Lab

By the end of the lab you have created three Python scripts that demonstrate using and importing functions, and the use of loops.

Exploration Questions

The following questions are for furthering your knowledge only, and may appear on quizzes or tests at any time later in this course.

  1. How do you declare a function?
  2. What are functions used for?
  3. What does return do?
  4. How can you import a function by name from another program?