5.25. Group Work: Functions and Conditionals¶
It is best to use a POGIL approach with the following. In POGIL students work in groups on activities and each member has an assigned role. For more information see https://cspogil.org/Home.
Note
If you work in a group, have only one member of the group fill in the answers on this page. You will be able to share your answers with the group at the bottom of the page.
Learning Objectives
Students will know and be able to do the following.
Content Objectives:
Recognize the keywords used in conditionals in Python (if, elif, else).
Predict the output from simple and complex conditionals.
Recognize the keywords used in complex conditionals (and, or, not).
Process Objectives:
Create good test data for functions with conditionals
Modify code to use complex conditionals
5.25.1. Basic Conditionals and Tests¶
Run this code to see what it prints.
Modify the code in the main method below to test all possible return values from get_temp_desc.
Put the blocks in order to define the function check_guess
which will return 'too low'
if the guess is less
than the passed target, 'correct'
if they are equal, and 'too high'
if the guess
is greater than the passed target. For example, check_guess(5,7)
returns
'too low'
, check_guess(7,7)
returns 'correct'
, and check_guess(9,7)
returns
'too high'
. There are three extra blocks that are not needed in a correct solution.
Finish the function last_half(str)
which
returns the last half of the characters from the passed string str
.
If str
has less than 2 characters then return the empty string ""
.
For example, last_half("a")
returns ""
, last_half("coal")
returns "al"
, and last_half("bye")
returns ye
.
Run this code to see what it prints. The modify it to work correctly. Next,
add code to the main
function to test each possible letter grade. It
should return A
if the score is
greater than or equal 90, B
if greater than or equal 80, C
if greater
than or equal 70, D
if greater than or equal 60, and otherwise E
.
5.25.2. Logical Operators and Complex Conditionals¶
The logical operators in Python are and
, or
, and not
. These can be used to create complex conditionals.
x > 10 and < 20
-
You have to check that x is greater than 10 and x is less than or equal 20. You must repeat the x.
x > 10 && x <= 20
-
Python uses the keyword and, not &&
x > 10 and x <= 20
-
This will be true when x is greater than 10 and less than or equal 20
x > 10 && x < 20
-
Python uses the keyword and, not && and this will be true when x is greater than 10 and less than 20
Q-12: Which of the following tests if x is both greater than 10 and less than or equal to 20?
have_ride and can_walk
-
Both must be true for this to be true
have_ride or can_walk
-
This will return true when one of these is true
have_ride || can_walk
-
Python uses the keyword or, not ||
have_ride OR can_walk
-
Python keywords are all lowercase
Q-13: Which of the following returns true if have_ride
is true or can_walk
is true?
! is_raining
-
Python does not use the ! symbol for not
not is_raining
-
This will return True when is_raining is false.
is_raining
-
If is_raining is false this will return False.
is_raining == False
-
This will return True when is_raining is False.
Q-14: Which of the following returns true if is_raining
is false? Pick all that are correct.
Modify this code to use a complex conditional instead. It should still pass all tests. It should only take four lines of code or less.
Modify this code to use a complex conditional instead. It should still pass all tests. It should only take four lines of code or less.
Drag the blocks from the left and put them in the correct order on the right to define a function squirrel_play
that takes a temp
(the temperature) and Boolean is_summer
that indicates if it is summer or not and returns True
if it is summer and the temperature is between 60 and 100 (inclusive). It also returns True
if it isn’t summer and the temperature is between 60 and 90 (inclusive). Otherwise, it returns False
.
If you worked in a group, you can copy the answers from this page to the other group members. Select the group members below and click the button to share the answers.
The Submit Group button will submit the answer for each each question on this page for each member of your group. It also logs you as the official group submitter.