Practice: Conditionals and String Manipulation

7.5.2. Practice: Conditionals and String Manipulation#

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.

TODO: Put your answer here

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

TODO: Put your answer here

String Manipulation#

Now, make a variable with your name in it, called my_name

# TODO: enter your code here

Write code to display the first letter of your name

# TODO: enter your code here

Write code to display the last letter of your name

# TODO: enter your code here

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 .

# TODO: enter your code here

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.

# TODO: enter your code here

Now, go back and modify the message string to see if the different if/elif/else options all work