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.

If you do work in a group you can copy the answers from this page to the other group members.

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.

Learning Objectives

Students will know and be able to do the following.

Content Objectives:

Process Objectives:

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 get_middle which returns the middle characters from the passed string. If the string has less than 3 characters then return the passed string. If the string has an odd length, then return the middle character. If the string has an even length, return the two middle characters. For example, get_middle('abc') returns 'b' and get_middle('abcd') returns 'bc'.

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.

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.

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.

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

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.

You have attempted of activities on this page