Name: - Partners: - Python Activity 6: Functions
Name: - Partners: - Python Activity 6: Functions
Learning Objectives
Students will be able to:
Content:
Explain the meaning and purpose of a function
Recognize a function definition, function header, and function call in a program
Combine the use of functions with if/else statements
Explain programs that use the same function multiple times
Use good test data for programs that include functions
Process:
Write code that includes function definitions and function calls
Write programs that incorporate functions and if/else statements
Prior Knowledge
Python concepts from Activities 1-5
Understanding of flowchart input symbols
Further Reading
Transitioning from Visual Logic to Python: Chapter 6
Flowchart
Python Activity 6: Functions
FYI: A function definition is the segment of code that tells the program what to do when the
function is executed. The first line of a function definition is known as the function
header
b. What Python keyword is used to indicate that a code segment is a function definition?
________________________________________________________________________
d. The name of the function is in the function header. What is the name of the function?
________________________________________________________________________
e. Examine the Python function. Explain the syntax required for a function: indentation,
etc.
________________________________________________________________________
________________________________________________________________________
b. The function call and the function definition each include a variable within the
parentheses. The variable in this role is known as an argument. What is the argument in
the function definition? What is its purpose?
________________________________________________________________________
________________________________________________________________________
32
Python Activity 6: Functions
c. In this example the argument in the function definition and the argument in the function
call have the same name. Is this required? _______________________________
d. Enter and execute the program. Verify your answer to question ‘c’ by changing the
variable name in the main program from radius to number. Do not change the variable
name in the function definition. Does the program still work? __________________
e. Write a line of code that calls the calculateArea function and sends the value “6” as the
argument. Add the line of code to the main program and execute it to be sure it works
properly. _______________________________________________________________
f. Add a second function to the program that calculates and prints the diameter of a circle,
given the radius as an argument (parameter). Place the function definition above the main
part of the program. Write the function below.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
g. Add another line of code to the main part of the program that calls the function that was
created in part ‘f’. Send the radius entered by the user as the argument to the function.
________________________________________________________________________
33
Python Activity 6: Functions
b. Enter and execute the program. Use the following test data and indicate what the output
is for each set of data.
c. What problems did you notice when you entered Data Sets 3 and 4?
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
d. Add code to the program that would warn the user about the problems that could occur
when data similar to that in Data Sets 3 and 4 are entered. See sample output below. List
the lines of code below the sample output.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
34
Python Activity 6: Functions
FYI: So far, the functions you have printed any results within the function. They did not send
back any information to the original calling code. Functions that do not send back
information to where they are called are known as void functions. Functions often send
back or return the result and are known as value returning functions.
5. Enter and execute the code below. Carefully examine the code.
a. What is the new keyword used in the function definition? What do you think the
keyword tells the program to do?
________________________________________________________________________
________________________________________________________________________
b. Write the line of code from the program that includes the function call to getSmaller.
________________________________________________________________________
c. In a void function, the function call is on a line by itself. Why is this function call
placed on the right-hand-side of an assignment statement?
________________________________________________________________________
________________________________________________________________________
d. What are the arguments used for the function call? _______________________________
35
Python Activity 6: Functions
7. The following Python program performs the same operations and produces the same output as the
previous program. Describe the major differences between the two programs. Edit the previous
program so that it looks like this one and execute it.
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
36
Python Activity 6: Functions
____________________________________________________________
____________________________________________________________
____________________________________________________________
____________________________________________________________
____________________________________________________________
____________________________________________________________
____________________________________________________________
____________________________________________________________
____________________________________________________________
____________________________________________________________
____________________________________________________________
____________________________________________________________
3. Write a Python program that prompts the user for three words and prints the word that comes last
alphabetically. Use a function to create the program.
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
37
Python Activity 6: Functions
38
Name: _______________________________________ Partners: ________________________________
Python Activity 7: Nested IF/ELSE Statements
“Decisions, decisions – decisions within decisions!”
Learning Objectives
Students will be able to:
Content:
Explain the purpose of a nested if-else statement
Explain how to use Python if-elif structure
Explain how to test code using Python if-elif structure
Process:
Write code that includes if-elif statements
Write code that uses if-elif statements and functions
Prior Knowledge
Python concepts from Activities 1-6
Understanding of flowchart input symbols
Further Reading
Transitioning from Visual Logic to Python: Chapter 7
Flowchart
Python Activity 7: Nested IF/ELSE Statements
a. Draw arrows between each flowchart symbol and the equivalent Python code.
b. In the Python code, circle the if/else statement that is nested within another if/else statement.
c. Enter and test the code. List five numbers that you would use to test this program. Indicate
why you chose the numbers.
2. Enter and execute the following Python program using the same data as you used for #1c.
a. How does the output for this program compare with the output for the previous program?
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
c. Notice the syntax of this program compared to the previous program. Which program
contains simpler indentation? _____________________________________________
FYI: elif is the Python keyword that represents else if and allows you to test for one of several options. As
soon as one of the tests is true, the rest are ignored.
d. You can use elif as many times as you need to. Suppose you wanted to add the comment
“Good!” for grades that are between 80 and 89. Where would you add it? Write the code for
this additional choice.
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
40
Python Activity 7: Nested IF/ELSE Statements
h. Change the program in #2 so that it prints the following messages. Write the code below.
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
i. Make a final change to the program so that it prints an error message if the grade entered
is greater than 100 or less than 0.
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
3. Is the use of the else statement mandatory when creating an if/elif statement? Explain your
answer.
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
41
Python Activity 7: Nested IF/ELSE Statements
2. Carefully examine and then complete the following Python program. The program prompts the user
to enter a number between 1 and 5 and also generates a random number between 1 and 5. The
program prints the number the user enters and prints the random number. The program then
compares the two numbers. If the numbers are the same, it prints the message “You picked the same
number as the computer!”. If the number the user entered is higher than the random number, the
program should print “Your number is higher than the computer’s number.” Otherwise, it should
print: “Your number is smaller than the computer’s number”.
42
Python Activity 7: Nested IF/ELSE Statements
43
Python Activity 7: Nested IF/ELSE Statements
44
Name: _______________________________________ Partners: ________________________________
Python Activity 8: Looping Structures – WHILE Loops
“Repeating code”
Learning Objectives
Students will be able to:
Content:
Explain the three parts of a loop
Explain the syntax of a while loop
Explain sentinel-controlled and counter controlled loops
Explain short-cut operators
Process:
Write code that includes sentinel-controlled and counter controlled loops
Write code that uses short-cut operators
Prior Knowledge
Python concepts from Activities 1-7
Understanding of flowchart input symbols
Further Reading
Transitioning from Visual Logic to Python: Chapter 8
a. Draw arrows between each flowchart symbol and the equivalent Python code.
b. In the Python code, circle all the code associated with the WHILE loop.
Python Activity 8: Looping Structures – WHILE Loops
c. Enter and test the code. What does the line of code: x=x+1 do?
___________________________________________________________________________
d. How does the Python interpreter know what lines of code belong to the loop body?
___________________________________________________________________________
e. Every loop structure requires three actions. Identify the line of code in the Python
program that corresponds to each of the three actions.
Initialize a variable used in the test condition:
________________________________________________________________________
Include a test condition that causes the loop to end when the condition is false:
________________________________________________________________________
Within the loop body, update the variable used in the test condition:
________________________________________________________________________
_______________________________
_______________________________
__________________________________________________
_______________________________
__________________________________________________
__________________________________________________
_______________________________
_______________________________
__________________________________________________
_______________________________
__________________________________________________
_______________________________
__________________________________________________
_______________________
__________________________________________________
a. Beside each line of code below explain what the code does.
3. The following code should print the numbers from 1 to 10, but it does not print anything.
Correct the problem.
number = 12
while number <= 10:
print(number)
number = number + 1
46
Python Activity 8: Looping Structures – WHILE Loops
7. We want to create a program that prompts the user to enter a number between 1 and 10. As long
as the number is out of range the program re-prompts the user for a valid number. Complete the
following steps to write this code.
a. Write a line of code the prompts the user for number between 1 and 10.
________________________________________________________________________
b. Write a Boolean expression that tests the number the user entered by the code in step “a.”
to determine if it is not in range.
________________________________________________________________________
c. Use the Boolean expression created in step “b.” to write a while loop that executes when
the user input is out of range. The body of the loop should tell the user that they entered
an invalid number and prompt them for a valid number again.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
d. Write a line of code that prints a message telling the user that they entered a valid
number.
________________________________________________________________________
e. Put the segments of code from steps “a-d” together. Enter and execute the code. Does it
work properly? If not, correct it and test it again. _________________________
47
Python Activity 8: Looping Structures – WHILE Loops
FYI: A looping structure for which you know the number of times it will execute is known as a
count-controlled loop.
8. Sometimes a programmer does not know how many times data is to be entered. For example,
suppose you want to create a program that adds an unspecified amount of positive numbers
entered by the user. The program stops adding numbers when the user enters a zero or a negative
number. Then the program prints the total. Before creating this program, review the three actions
required for all loops:
a. Initialize a variable that will be used in the test condition: What will be tested to
determine if the loop is executed or not? Write a line of code that initializes a variable to
be used in the test condition of the loop for this program. The variable should contain a
value entered by the user.
________________________________________________________________________
b. Include a test condition that causes the loop to end when the condition is false: What is
the test condition for the while loop used in this program?
________________________________________________________________________
c. Within the loop body, update the variable used in the test condition: Write the code for
the loop body. Include the code to update the variable in the test condition.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
e. Complete the program. Enter and execute the code. Does it work properly? __________
FYI: Short-cut operators provide a concise way of creating assignment statements when the
variable on the left-hand side of the assignment statement is also on the right-hand side. The
addition short-cut operator (+=) is usually used for incrementing a variable.
48
Python Activity 8: Looping Structures – WHILE Loops
c. Replace the operator ‘+=’ with the following shortcut operators and execute the code.
Explain what each operator does.
-= _________________________________________________________
*= _________________________________________________________
c. As a result of correcting this code segment, what is needed in order for the shortcut
operators to work properly?
_______________________________________________________________________
11. The following code should print the numbers beginning at 100 and ending with 0. However it is
missing a line of code. Add the missing code, using the shortcut operator. Indicate where the
code belongs.
countdown = 100
while countdown > 0:
print(countdown)
print(“Done!”)
________________________________________________________________________
b. What is the name of the variable used to store the user’s input? ____________________
49
Python Activity 8: Looping Structures – WHILE Loops
FYI: A sentinel-controlled while loop is a loop that repeats the loop body until the user enters a pre-
specified value.
What type of data is stored in each variable: (integer, floating point, or string)
name – __________________________
cost – __________________________
numApples – __________________________
FYI: A variable that can store only the values True and False is called a Boolean variable.
50
Python Activity 8: Looping Structures – WHILE Loops
d. Experiment with the arguments in the max() function in the program to determine if the
function must have four arguments. Demonstrate your answer.
________________________________________________________________________
________________________________________________________________________
2. Write code segment that prompts the user for a letter from ‘a-z’. As long as the character is not
between ‘a-z’, the user should be given a message and prompted again for a letter between ‘a-z’.
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
51
Python Activity 8: Looping Structures – WHILE Loops
52