23.4. Ch 7 Practice: Conditionals and String Manipulation#
23.4.1. IF ELSE Statements#
Look at the following code example and try running it
x = 5
if x < 11:
print('A')
elif x >= 13:
print('B')
else:
print('C')
A
Now what would happen if x was changed to 30? What would the output be?
Do Not Code. Please answer this question as a markdown block. Modify the code above to check your answer afterwords.
It would print ‘B’
What value would the x
variable have to be in order to get the output ‘C’?
Again, Do Not Code. Please answer this question as a markdown block Modify the code above to check your answer afterwords
If x were 12, it would print C
23.4.2. String Manipulation#
Now, make a variable with your name in it, called my_name
my_name = "Kyle"
Write code to display the first letter of your name
display(my_name[0])
'K'
Write code to display the last letter of your name
display(my_name[-1 : ])
'e'
23.4.3. Ifs with Strings#
Save a string in a variable called message
and have it end with either a question mark ?
, an exclamation mark !
, or a period .
message = "This is an example message!"
Now, make a set of if/elif/else statements which will display either:
The message was a question
The message was an exclamation
The message was a statement
The message ended unexpectedly
depending on what character the message ended in a ?
, !
, .
, or something else. Hint: use the endswith
function.
if message.endswith("?"):
display("The message was a question")
elif message.endswith("!"):
display("The message was an exclamation")
elif message.endswith("."):
display("The message was a statement")
else:
display("The message ended unexpectedly")
'The message was an exclamation'
Now, go back and modify the message string to see if the different if/elif/else options all work