Skip to main content

Worksheet 5.42 Group Work: Functions with Strings 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 5.42.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.

Section 5.42.1 Learning Objectives

Students will know and be able to do the following.
Content Objectives:
Process Objectives:

Subsection 5.42.1.1 Function Example

A function is a name for one or more lines of code. You first define a function using the def keyword and then execute it using function_name(arguments).

Activity 5.42.1.

Activity 5.42.2.

Run the code below to see what it prints and then use it to answer the following questions.

Note 5.42.2.

You do not declare the type of a variable in Python. Python requires that all statements that are part of the body of a function must be indented. Use four spaces to indent.

Activity 5.42.3.

Activity 5.42.4.

Activity 5.42.5.

Drag the blocks from the left and put them in the correct order on the right to do the following:
  • First define the function print_greeting
  • In the print_greeting function ask for the user’s name
  • Print "Hello " followed by the name
  • Define a main function
  • In the main function, call print_greeting
  • Call the main function
Note that you will have to indent the lines that are in the body of each function. Click the Check button to check your solution.

Subsection 5.42.1.2 String Definition

A string is a sequence of characters enclosed in quotes. In Python you can use pairs of single or double quotes to enclose a string like in "hello" or 'hello'. This is especially useful when a string includes a single quote "they're". You can even use tripe quotes when a string covers more than one line.

Subsection 5.42.1.3 String Indices

You can get a character from a string at an index (position) using string[index].

Activity 5.42.6.

Activity 5.42.7.

Run the code below to see what it prints. Then fix it to pass the test shown below the code. It should return a string with the first character of the first name and first character of the last name.

Note 5.42.3.

Use string[index] to get a character from a string. The first character in a string is at index 0 and the last is at the length of the string minus 1 (also know as index -1 in Python)

Subsection 5.42.1.4 String Slices

You can get a copy of part or all of a string using str_name[start:end].

Activity 5.42.8.

Activity 5.42.9.

Run the code below to see what it prints.

Note 5.42.4.

Use the slice [start:end] operator to get a slice (substring) from a string. It will return a new string starting at the start index and include all the characters up to just before the end (the last character will be from index end - 1). If start is missing the default is 0 and if end is missing the default value is the length of the string.

Activity 5.42.10.

Activity 5.42.11.

Subsection 5.42.1.5 Basic Conditionals and Tests

You can execute code only when a condition is true using if. You can execute one block of code when a condition is true (using if) or false (using else). You can even speicfy more than two outcomes as shown in the code below.

Activity 5.42.12.

Activity 5.42.13.

Run this code to see what it prints.

Note 5.42.5.

You must first convert a number to a string using str(number) before you add it to a string using +.

Activity 5.42.14.

Activity 5.42.15.

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

Activity 5.42.16.

Put the blocks in order on the right to do the following:
  • First define the function check_guess
  • In the function, check if the guess is too low and if so return "too low"
  • Otherwise, check if the guess is correct, and if so return "correct"
  • Else return ’too high’
There are extra blocks that are not needed in a correct solution.

Activity 5.42.17.

Activity 5.42.18.

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.

Activity 5.42.19.

Subsection 5.42.1.6 Logical Operators and Complex Conditionals

The logical operators in Python are and, or, and not. These can be used to create complex conditionals.

Activity 5.42.20.

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.

Activity 5.42.21.

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.
You have attempted of activities on this page.
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.