Skip to main content

Section 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 1 .

Note 5.25.1.

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

Subsection 5.25.1 Basic Conditionals and Tests

Checkpoint 5.25.2.

Q-1: What is the first thing (first line of text) that will be printed when the code below runs?

Checkpoint 5.25.3.

Run this code to see what it prints.

Checkpoint 5.25.4.

Q-3: What keyword specifies the block of statements to execute if a Boolean condition is false?

Checkpoint 5.25.5.

Q-4: What keyword starts a conditional statement and is the only required keyword in it?

Checkpoint 5.25.6.

Q-5: What keyword is used in a conditional statement when you want three of more possible outcomes?

Checkpoint 5.25.7.

Modify the code in the main method below to test all possible return values from get_temp_desc.

Checkpoint 5.25.8.

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.

Checkpoint 5.25.9.

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.

Checkpoint 5.25.10.

Q-9: What is the first thing that will be printed when the code below runs?

Checkpoint 5.25.11.

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.

Checkpoint 5.25.12.

Q-11: How many test cases do you need to check that the code above works as intended?

Subsection 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.

Checkpoint 5.25.13.

    Q-12: Which of the following tests if x is both greater than 10 and less than or equal to 20?
  • 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

Checkpoint 5.25.14.

    Q-13: Which of the following returns true if have_ride is true or can_walk is true?
  • 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

Checkpoint 5.25.15.

    Q-14: Which of the following returns true if is_raining is false? Pick all that are correct.
  • ! 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.

Checkpoint 5.25.16.

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.

Checkpoint 5.25.17.

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.

Checkpoint 5.25.18.

Put the code below in order to first check if it is your birthday and if so then if the speed is less than or equal 65 return 0, else if it is less than or equal 85 return 1 and otherwise return 2. If it isn't your birthday then if the speed is less than or equal 60 return 0, else if it is less than or equal 80 return 1 and otherwise return 2.

Checkpoint 5.25.19.

Write a function that given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, …6=Sat, and a Boolean indicating if we are on vacation, return a string of the form “7:00” indicating when the alarm clock should ring. If you are on vacation then on weekends it should be “off” and weekdays “10:00”. Otherwise, on weekends it should be “10:00” and weekdays “7:00”.

Checkpoint 5.25.20.

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.
<div class="runestone sqcontainer %(optclass)s"> <div data-component="groupsub" id=func_cond_groupsub data-size_limit=4> <div class="col-sm-6"> <select id="assignment_group" multiple class="assignment_partner_select" style="width: 100%"> </select> </div> <div id="groupsub_button" class="col-sm-6"> </div> <p>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.</p> </div> </div>
https://cspogil.org/Home