A condition on its own only produces True or False. To make the computer
actually do something different depending on that answer, the condition has to
be attached to an if. That is selection: the code that makes the computer
perform different tasks in different situations. It is sometimes called a
decision, which is a good way to think about it.
The simplest form of selection has a single outcome. Something either happens or it does not.
weather = "rain"
if weather == "rain":
print("Take your umbrella")
Take your umbrella
There are three parts to that piece of code, and all three are needed:
if, followed by the condition.if line. It marks the end of the condition
and announces that a block of code is coming.Now change one thing, the value stored in weather:
weather = "sun"
if weather == "rain":
print("Take your umbrella")
This time the program appears to do nothing at all. It produces no output and no
error. That is not a bug: the condition came back False, so Python skipped the
indented line underneath it, and there was nothing else left in the program to
run.
A program with an if and nothing else can finish without printing anything.
When that happens, the condition was false and the branch was skipped. It is
easy to think the code is broken when it is doing exactly what you asked.
Indentation is not decoration in Python. It is how the language knows where the branch stops. Compare these two programs, which differ only in how far the last line is indented.
Here the second print is indented, so it is part of the branch and is skipped
along with the first one:
weather = "sun"
if weather == "rain":
print("Take your umbrella")
print("Off to school")
The program outputs nothing.
Here the second print starts at the left margin, so it is not part of the
branch. It sits after the if and runs whatever the weather is:
weather = "sun"
if weather == "rain":
print("Take your umbrella")
print("Off to school")
Off to school
Indent every line of a branch by the same amount. Four spaces is the usual choice in Python, and the editor adds them for you when you press enter after a line that ends in a colon.
One if gives you one outcome. Very often you want two: one thing when the
condition is true, and a different thing when it is false. That is what else
is for.
mark = 55
if mark >= 40:
print("You passed")
else:
print("You failed")
You passed
else is a catch all. When the condition after the if is true, Python runs
the indented lines under the if and skips straight past the else. When the
condition is false, Python skips the if branch and runs the indented lines
under the else instead. Exactly one of the two branches runs, every single
time.
The else line needs a colon and an indented branch of its own, just like the
if does.
else never carries a condition. Writing else mark < 40: is an error.
It does not need one, because there is nothing left to check. else means “in
every other case”, and every other case is precisely what is left over when the
if condition is false.
The colon is the part people leave off most often. Python cannot guess where the condition ends without it, so it stops before running anything:
SyntaxError: expected ':'
If a program refuses to start and the error mentions a colon, look at the end of
your if and else lines.
Run the code below and type rain when it asks. Then run it again and type
sun, and then Rain with a capital R.
print("What is the weather like?")
weather = input()
if weather == "rain":
print("Take your umbrella")
else:
print("No special advice for you")
Once all three answers make sense, delete the else line and the line under it,
and run the program again with rain and with sun. With the else gone you
are back to one outcome, so one of those two answers now produces no output
beyond the question itself.
Make sure that you check for the following things:
:) at the end of every if line and every else line.else.==) in a condition that checks whether
two pieces of data are the same. A single = stores data in a variable.input.