In the last module, you learned about variables and expressions, which will be extremely important in this module. If you’re still a little iffy on these concepts (especially expressions), you’ll probably want to go back and review. We’re about to talk about conditionals, and conditionals are all about boolean expressions.
We also introduced debugging in the last module. Once you get past the intial annoyance of an error message, debugging can be like puzzle; it’s the game of figuring out what’s going wrong, why, and how to fix it. Debugging in Python is especially pleasant because Python provides clear, helpful error messages. There’s more debugging to come for you, both in this and future modules — and in your future life as a coder.
These concepts — data types, printing, variables, expressions and conditionals — aren’t just stepping stones that you’ll never see again. As a programmer, you’ll use each of these every day (print statements are especially useful for debugging). As you develop a solid understanding of these concepts, you’re building a strong foundation for yourself as a coder.
In order to fully understand conditional logic, it’s important to have a solid
grasp of boolean expressions in python. Remember, a boolean expression is a
statement or a part of a statement that results in a Boolean value, such as
True
or False
. Comparisons are the most obvious and helpful example
of boolean expressions:
>>> "rainbow rockstar" == "mermaid unicorn"
False
This expression, which is the comparison of two strings, results in the Boolean value False. We can also compare two numbers. Read the following boolean expressions:
>>> 5 == 5
True
>>> 4 > 1
True
>>> 7 > 99
False
Also, remember that True
and False
are values in their own right. If
we wanted to simply make a variable whose value is True
, we could:
>>> my_var = True
>>> print my_var
True
Also, reaching way back to the first module, the string "True"
is not
the same as the Boolean value True
. They are two values that look the same,
but are different types.
As a fun aside, we could prove this using a Boolean expression:
>>> type("True") == type(True)
False
>>> "True" == True
False
>>> "True" != True
True
We’re about to add a very useful tool to our toolkit. Conditional logic lets us set conditions, or prerequisites, in our code, such that only certain lines of code are run if certain conditions are met. This is similar to allowing yourself to go to the movies if you finish this module of work. Some “psedocode”, or “fake” computer code, for you day might look like this:
work on module
if module is finished:
go to the movies, have lots of fun!
otherwise:
stay home and keep working.
In our pseudocode, the line that says if module is finished
is called an
if-statement. Python if-statements look pretty similar to our pseudocode.
Here is some valid Python code that does a similar thing:
module = "incomplete"
if module == "complete":
print "Time to go to the movies"
else:
print "Stay home and keep working."
Try typing this out in your Python repl console on your own. What is the output of this code?
Here is the output
The output is that the string "Stay home and keep working."
has been
printed.
>>> module = "incomplete"
>>> if module == "complete":
... print "Time to go to the movies"
... else:
... print "Stay home and keep working."
...
Stay home and keep working.
if family_member1 == family_member2:
print "You have two family members with the same name."
There are a couple important things to note about the correct way to craft an if-statement.
if
is another Python keyword. It must be lower-cased.if
is a Boolean expression followed by a colon– :
.True
.The if-statement body must be indented
In order for the if-statement to be considered to be valid Python syntax, it must be indented! That means, include 4 blank spaces, and then the actual code that should get run
The if-statement body can be multiple lines
You can include more than one line of code in an if-statement’s body. Here’s an example:
if family_member1 == family_member2:
print "You have two family members with the same name."
print "Here is family member 1: ", family_member1
print "Here is family member 2: ", family_member2
if message_recipient == "Balloonicorn":
print "Balloonicorn, you have a message."
else:
print "No messages for you, Balloonicorn!"
elif
If there are multiple successive conditions you’d like to check, you can utilize
the elif
keyword. Here’s an example:
if message_recipient == "Balloonicorn":
print "Balloonicorn, you have a message."
elif message_recipient == "Hackbright":
print "Ballonicorn, can you take a message for Hackbright?"
else:
print "No messages for you, Balloonicorn!"
The elif
keyword is a way to evaluate an additional condition, after the first
has been checked.
It’s important to note that if the first if-statement evaluates to True
,
neither the elif
, nor the else
, will ever occur. With just an if
and an else
, it’s either one or the other. With an if
, elif
, and
and else
, it’s only one of the 3 conditions that can occur.
The conditions are checked in the order that they appear: first the if
, then
the elif
, then, if neither the if
nor the elif
worked out, the else
handles it.
If-statements can be nested. That is, another if-statement can be included in the body of an if-statement. Here is an example:
num_pets = 5
fav_animal = "cat"
if num_pets > 5:
print "Wow, that's a lot of pets!"
if fav_animal == "cat":
print "I like cats too!"
Nested if-statements can also have elif
blocks and else
blocks. To complicate the
example above a bit:
num_pets = 5
fav_animal = "cat"
if num_pets > 5:
print "Wow, that's a lot of pets!"
if fav_animal == "cat":
print "I like cats too!"
else:
print "Why don't you like cats??"
elif num_pets == 2:
print "I hope your two pets are friends."
elif num_pets == 0:
print "Time to get a pet."
So far, our if-statements have been a little bit contrived. What’s the point of checking to see if a variable is equal to another thing if you made the variable yourself, right? If statements are typically used when the result of that Boolean expression is unknown, or varying over time or depending on some input that was provided to a program.
As luck would have it, we do have a function that can take in input into our programs– raw_input!
Using these two concepts, we can make a little advice machine:
>>> print "Would you like some advice?"
>>> print "If yes, type Y."
>>> the_answer = raw_input("What's your answer? ")
>>> if the_answer == "Y":
... print "A wise person knows when to ask for help."
... else:
... print "Ok, I won't give you any advice then."
Open a repl Python console and type this out. Remember when this line runs:
>>> the_answer = raw_input("What's your answer? ")
You’ll have to provide the answer yourself. Try it out with two different inputs.
So, the first time, type Y in response to What's your answer?
, and then
type something other than Y.
Login to your account and start a new repl here. Title your new session module_3_practice.py.
Complete the practice problems below in this repl console. If you’d like to work through the practice in several sittings be sure to log in to Repl.it and save your work in between.
adjective = "absolutely fabulous" adjective2 = "supercalifragilisticexpialidocious" noun = "aardvarks" noun2 = "billy goats" verb = "lollygagging" verb2 = "jogging"
Write an if-statement for each of the above variables. The if-statement should check whether the length of the variable’s value is greater than 9 characters. If it is, your code should print “long string”. If it’s not, print “not a long string”.
Here’s a hint:
Here’s the first part. It accomplishes half of the problem. However, it doesn’t accomplish printing “not a long string” when the length is not greater than 9 characters.
if len(adjective) > 9:
print "long string"
Using the raw_input function, prompt yourself to type something. Be sure to capture what you type into a variable. Once you have the variable, print the length of whatever you typed.
Type the following into the repl Python console:
>>> answer = raw_input("What is 2 + 2 ?")
>>> answer_as_integer = int(answer)
Then, write some code that follows these specifications:
Do this a couple of times, making sure that if you type 1) the wrong answer 2) a “too high” answer and 3) a “too low” answer all show the correct output.
Write a conditional statement that checks if the variables verb and verb2 are equal to one another. If they are, print “They are equal!”. If they aren’t, print 3 things: the value of verb, the value of verb2, and the message “These are not the same”
Type the following code to prompt the user to rate the movie Wizard of Oz:
>>> rating = raw_input("On a 1-10 scale, how would you rate Wizard of Oz?")
>>> rating_as_integer = int(rating)
Then, write an if/elif/else statement that follows these specifications:
As a programmer, debugging is a fact of life. There are times you write code that Python doesn’t understand. In these cases, Python will display an error message. The more familiar you are with Python’s many error messages, the faster you’ll be at debugging code. But there’s good news: Python’s error messages are incredibly descriptive and helpful in figuring out what the problem is.
In the following problems, you’ll find code that is invalid or not allowed in some way. Read the code, and see if you can predict what is wrong. When you’re ready, hover over the solution area to reveal the error message that Python shows, along with an explanation of what is going wrong.
1) What’s wrong with this code?
>>> rating = 5
>>> if rating = 5:
... print "Right in the middle."
Must use double-equals sign in if-statement
Since the condition for an if-statement is a boolean expression, you must include 2 equals signs to compare equality in an if-statement.
This code would throw a SyntaxError. Python even points out the single equals sign as the origin of the problem for us!
>>> if rating = 5:
File "<stdin>", line 1
if rating = 5:
^
SyntaxError: invalid syntax
rating = 5
if rating == 5:
print "You rated a 5!"
Missing indentation for if-statement body
The if-statement body must be indented.
>>> if rating == 5:
... print "You rated a 5!"
File "<stdin>", line 2
print "You rated a 5!"
^
IndentationError: expected an indented block
Thankfully, Python is very good at showing us the problem. The next block of code was expected to be indented.
Create a new repl called module_3_user_questions.py.
Ask the user as series of questions using raw_input, capturing their input into appropriately-named variables. The questions should be
At the end of your program, print a summary of all of the answers to their questions.