Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
5.41. Group Work: Functions, 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.
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.
Students will know and be able to do the following.
Learn how to display a value in Python using print.
Match terms for functions to their definitiions.
Recognize common symbols (:, #), operators (+, -, /), and keywords (def, return) in Python.
Use string indices.
Understand how slice works for both positive and negative indices.
Use input and convert between strings and numbers.
Recognize common string methods.
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).
Learn how to properly indent the body of a function
Predict the output from code.
Modify code to produce the correct output.
Create good test data for functions with conditionals
Modify code to use complex conditionals
5.41.1. Function Example¶
Run the Python code below and then answer the following questions.
Run the code below to see what it prints and then use it to answer the following questions.
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.
Delete the last line of the test function above and run the code again.
Drag the blocks from the left and put them in the correct order on the right to define a function
print_greeting that asks for your name and prints “Hello Name”. Then define a
main function that calls
print_greeting. Be sure to also 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.
5.41.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'. 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.
5.41.3. String Indices¶
Run the code below to see what it prints. Then fix it to pass the given test. It should return a string with the first character of the first name and first character of the last name.
Use [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).
Fix the function
get_initials above to return a string with the first letter of the first name followed by the first letter of the last name.
5.41.4. String Slices¶
Run the code below to see what it prints.
Use the slice
[start:end] operator to get a slice (substring) from a string. It will return a new string starting at the start and including all the characters up to just before the end (end - 1).
start is missing the default is 0 and if
end is missing the default value is the length of the string.
5.41.5. 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.
You must first convert a number to a string using
str(nun) if you want to add it to a string using
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,
'too high'. There are three extra blocks that are not needed in a correct solution.
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.41.6. Logical Operators and Complex Conditionals¶
The logical operators in Python are
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.
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.