Using Decisions with Strings¶
Learning Objectives:
- Give examples of programs that use conditions with strings.
- Use the
input
function to get input from a user - Introduce using
elif
to handle 3 or more options. - Introduce using
str
to convert a number to a string.
Here’s an example of conditional execution using if
statements with
strings. We can print different strings based on the value of a number. For
example, if the user only orders 1 item we can print that out differently than
if the user orders more than one item. Notice the use of str(num_items)
in
the code below. The function str
turns a number into a string so that it
can be appended to a string.
Change the value of num_items
to see how that changes the output.
-
csp-13-1-1: What will be printed if num_items = -1?
- You ordered 1 item
- This line will only print when the numer of items is 1.
- Your ordered -1 items
- This line will print whenever the number of items is greater than 1.
- Nothing will be printed.
- If num_items is negative neither of the if's will be true so the variable message will not be created.
- You will get an error message that message is not defined.
- The variable message won't be created if the number of items is less than 1, so trying to print the value of message will cause an error.
-
csp-13-1-2: What will be printed if you change the second if to an else and set num_items = -1?
- You ordered 1 item
- This line will still only print when the numer of items is 1.
- Your ordered -1 items
- This line will print whenever the number of items was not equal to 1 when you change the second if to an else.
- Nothing will be printed.
- If num_items is negative neither of the if's will be true so the variable message will not be created.
- You will get an error message that message is not defined.
- The variable message won't be created if the number of items is less than 1, so trying to print the value of message will cause an error.
What if you want different messages to be printed based on the user’s score in a game? The code below should print “You can do better!” if the score is less than 10, “Good job” if the score is between 10 and 19 inclusive, and “Amazing” if the score is 20 or more, but it needs to be fixed. First run it with different values to see what happens and then answer the multiple choice questions below.
-
csp-13-1-3: What is printed when the score is 10?
- You can do better.
- This line will only print when score is less than 10.
- Good job!
- This line will only print whenever the score is more than 10.
- Amazing!
- This line will only print whenever the score is more then 20.
- Nothing is printed
- When score equals 10 none of the current if statements will be true, so nothing is printed.
-
csp-13-1-4: What is the first thing printed when the score is 25?
- You can do better.
- This line will only print when score is less than 10.
- Good job!
- This line will print whenever the score is more than 10, so if the value is 25 it will print first. And then it will also print "Amazing!".
- Amazing!
- This line will print whenever the score is more than 20, but another line will print first.
- Nothing is printed
- The value 25 is more than 10.
Now go back and change the last active code (csp_sd_score) to work correctly.
Remember that you can use and
to join two logical expressions. This is
especially useful if you want to test if a number is in a range of numbers like
10 to 19 inclusive. So change the example to print the first thing if less
than 10, the second thing if it is between 10 and 19 and the third thing if it
is 20 or more.