Return to Video

Python for Informatics - Chapter 3 - Conditional Execution

  • 0:00 - 0:05
    Hello, and welcome to Chapter Three of
    Python for Informatics.
  • 0:05 - 0:07
    Chapter One, Chapter Two, now we're
  • 0:07 - 0:09
    going to get to something kind of
    programmy.
  • 0:09 - 0:16
    I mean, assignment statements and reserved
    words, that just kind of gurgling.
  • 0:16 - 0:18
    Now we're going to start seeing
    composition.
  • 0:18 - 0:21
    We're going to start seeing the
    conditional execution.
  • 0:21 - 0:23
    Gets us started, sort of, seeing the power
  • 0:23 - 0:26
    of computers, where you're starting to
    make decisions.
  • 0:26 - 0:31
    So, as always, this lecture and audio,
    video, and slides are also available,
  • 0:31 - 0:35
    are copyright, Creative Commons
    Attribution.
  • 0:35 - 0:40
    So, conditional steps are steps that may
    or may not be executed.
  • 0:40 - 0:43
    So here's, here's a bit of code.
  • 0:43 - 0:46
    So, and, and I draw these pictures. I won't
  • 0:46 - 0:48
    draw too many of these pictures on the
    left-hand side.
  • 0:48 - 0:51
    If you've taken a programming class, you
    may have seen these.
  • 0:51 - 0:53
    They're sometimes called flow charts.
  • 0:53 - 0:55
    Sometimes people really think these are
    important.
  • 0:55 - 0:59
    I don't think they're all that
    important for understanding.
  • 0:59 - 1:00
    The, the Python code is here on the
  • 1:00 - 1:03
    right-hand side, and this picture's on the
    left-hand side.
  • 1:03 - 1:08
    And, and the reality is is that this may,
    initially, make more sense, cognitively,
  • 1:08 - 1:14
    to you, than this. But this part on the
    right-hand side is what's important.
  • 1:14 - 1:16
    I like to call these like road maps, so
    you can sort of
  • 1:16 - 1:20
    trace where the code is going by driving
    down a little road.
  • 1:20 - 1:22
    That's kind of a, something that you do
    once or
  • 1:22 - 1:25
    twice and then, pretty soon, you'll
    start reading the code.
  • 1:25 - 1:26
    So I'm going to start on the right-hand
  • 1:26 - 1:29
    side here, and just walk through the
    code.
  • 1:29 - 1:31
    Remember, code operates in sequence.
  • 1:31 - 1:37
    Well, there is a if, which is a special
    reserved word.
  • 1:37 - 1:40
    It's one of those things that you can't,
    you can't name a variable if.
  • 1:41 - 1:45
    And it is our indication that to Python,
    that the next
  • 1:45 - 1:50
    statement that we're going to do may or
    may not be executed, if.
  • 1:50 - 1:53
    And the thing that comes on the same line
    as the if,
  • 1:53 - 1:57
    up to and including the, the little colon,
    is a question.
  • 1:57 - 1:59
    This is a question.
  • 1:59 - 2:01
    You're asking a question.
  • 2:01 - 2:05
    So an assignment statement is moving a
    value into a variable.
  • 2:05 - 2:06
    And a if statement
  • 2:06 - 2:08
    is asking a question.
  • 2:08 - 2:10
    In this case, we're asking a question
    about a variable.
  • 2:10 - 2:16
    So always think, when you're, sort of,
    here, that this is a question to be asked.
  • 2:16 - 2:18
    And, you'll notice when I'm doing the same
  • 2:18 - 2:20
    thing over here, I put a question mark
    there.
  • 2:20 - 2:23
    Is x less than 10?
    Yes or no?
  • 2:23 - 2:25
    It's a question that has a yes or no.
  • 2:25 - 2:28
    And so, the way this works is, this
    statement
  • 2:28 - 2:32
    that's indented, after the if, is either
    executed or
  • 2:32 - 2:34
    not executed based on the result of that
    question.
  • 2:34 - 2:38
    So the way to sort of read this in
    English is set x to 5.
  • 2:38 - 2:44
    If x is less than 10, which it is because
    x is 5, then we're going to execute this.
  • 2:44 - 2:46
    So print Smaller comes out.
  • 2:46 - 2:49
    And then we come back out and we continue
    and say, oh, okay, now
  • 2:49 - 2:52
    I have another if statement, and then a
    bit of, a block of indented code.
  • 2:52 - 2:56
    If x is less than 20, that's the question.
  • 2:56 - 2:57
    The answer to that
  • 2:57 - 3:01
    is no, and so it does not run that line,
    and so it runs Finis.
  • 3:01 - 3:05
    So the printout of this program is
    Smaller, followed by Finis.
  • 3:05 - 3:11
    What happens is, this line never executes
    because the answer
  • 3:11 - 3:17
    to this question is false. Okay?
    So, let's go through that a little faster.
  • 3:17 - 3:21
    Set x to five.
    If x is less than 10, print Smaller.
  • 3:21 - 3:22
    Then, if x is greater
  • 3:22 - 3:26
    than 20, which it's not, skip that, and
    then print Finis.
  • 3:26 - 3:28
    That's the short version of it, okay?
  • 3:28 - 3:30
    Conditional steps.
  • 3:30 - 3:32
    This step is conditional, this step is
    conditional.
  • 3:32 - 3:36
    They may or may not be executed based on
    the result of the question.
  • 3:36 - 3:39
    Now, if we're thinking of this as like a
    GPS
  • 3:39 - 3:42
    road map or something, we can look at this
    right-hand side.
  • 3:42 - 3:47
    So, the CPU comes roaring down
    here, x equals 5, okay, I'll run that.
  • 3:47 - 3:52
    Then it's faced with a choice.
    Do, is x less than 10, yes or no?
  • 3:52 - 3:56
    If it is yes, and it is, I will go this
    way.
  • 3:56 - 3:58
    If it was no, I would go that way.
  • 3:58 - 4:01
    So if it's yes, I go here and I run this
    little thing and I print Smaller, great.
  • 4:01 - 4:02
    And I follow the little road.
  • 4:02 - 4:06
    And now the road takes me to here.
    And it's asking another question.
  • 4:06 - 4:08
    Is x greater than 20?
  • 4:08 - 4:12
    This time, the answer is no, so I'd come
    down here, right?
  • 4:12 - 4:12
    And so,
  • 4:12 - 4:15
    this bit of code is never executed.
  • 4:15 - 4:20
    Now, this is a very simple example, but
    you get the basic idea.
  • 4:20 - 4:22
    Okay? So that's conditional execution.
  • 4:22 - 4:27
    Now there's a number of conditional
    operators that we
  • 4:27 - 4:30
    want to use, just like we had
    multiplication, division.
  • 4:30 - 4:34
    Some of them are pretty intuitive,
    and the others, you just kind of have
  • 4:34 - 4:37
    to memorize, like less than and
    greater than make a lot of sense.
  • 4:38 - 4:42
    The one that probably, that, easy, like
    less than or equal to,
  • 4:42 - 4:45
    or greater than or equal to, those
    kind of make sense, too.
  • 4:45 - 4:47
    They're less than or equal to.
  • 4:47 - 4:50
    Just because we don't have a less than or
    equal to sign on a
  • 4:50 - 4:54
    symbol or a greater than or equal sign,
    which we would use in mathematics.
  • 4:54 - 4:57
    Equality, asking the question of
    whether something is
  • 4:57 - 5:00
    equal to something else or not,
    is double equal.
  • 5:00 - 5:04
    And that's because we're already using
    single equals as assignment.
  • 5:04 - 5:10
    So when we say x equals 3, that is an
    assignment and sticks a value into x.
  • 5:10 - 5:12
    This is the question.
  • 5:12 - 5:14
    Is x equal to?
  • 5:14 - 5:16
    If I was building a language, I would make
  • 5:16 - 5:19
    it be equal question mark, or something
    like that.
  • 5:19 - 5:22
    I'd be like, huh, is it equal?
    Kind of a question mark.
  • 5:22 - 5:23
    But that's not what we do.
  • 5:23 - 5:26
    I didn't invent this, so we're, double
    equals
  • 5:26 - 5:30
    is the question, is something equal to
    another.
  • 5:30 - 5:35
    Single equals changes something, x equals
    five changes x.
  • 5:35 - 5:40
    Okay, and then, not equal, exclamation is
    commonly used to mean not in
  • 5:40 - 5:45
    computer contexts, so if something
    is not equal to something,
  • 5:45 - 5:50
    it is exclamation equal.
    Here are some examples.
  • 5:50 - 5:52
    Just kind of running through them.
  • 5:52 - 5:55
    They're all, they all turn out to be true,
    because I set x to 5.
  • 5:55 - 5:58
    If x equals 5, print Equals 5.
  • 5:58 - 6:02
    Come out here, if x is greater than 4,
    which is true, print Greater than 4.
  • 6:02 - 6:05
    If x greater than or equal to 5, yep.
  • 6:05 - 6:08
    If x less than 6, print Less than 6.
  • 6:08 - 6:12
    Now here's a, there are two, sort of,
    syntaxes to, to the if statement.
  • 6:12 - 6:16
    One is where the if statement is down here
    on a separate line and it's indented,
  • 6:16 - 6:20
    and the other is where there's a single
    line and it's right on the same line,
  • 6:20 - 6:23
    if x less than 6, print Less than 6.
  • 6:23 - 6:26
    So this is true, so this whole thing
    executes.
  • 6:26 - 6:29
    Then it continues down, if x less than or
    equal to 5?
  • 6:29 - 6:30
    Yep, print Less than or Equal 5.
  • 6:30 - 6:35
    If x is not equal to 6, which is true, cuz
    it's 5, then Not equal to 6.
  • 6:35 - 6:40
    So, all those will turn out to be true,
    and all those will execute.
  • 6:40 - 6:46
    And so, the, the tricky bit, here, is, you
    know, just knowing,
  • 6:46 - 6:50
    seeing this syntax for an if statement,
    where it's all one line, and this syntax,
  • 6:50 - 6:53
    where you end the first line with a colon
    and then indent the second line.
  • 6:54 - 6:56
    This, you can only do one line.
  • 6:56 - 6:59
    We will soon see that you can put more
    than one line in the indented block.
  • 7:00 - 7:02
    Okay.
  • 7:03 - 7:05
    Here we have more than one int line in
  • 7:05 - 7:09
    the indented block, these are called
    one-way decisions.
  • 7:09 - 7:11
    And so, we say x equals 5,
  • 7:11 - 7:14
    we print out Before 5, so that
    prints out.
  • 7:14 - 7:20
    If x equals 5, remember the double equals
    is the question mark version of equality,
  • 7:20 - 7:22
    single equals assignment, it says yes.
  • 7:22 - 7:25
    So we indent, and the convention is to
    indent four spaces,
  • 7:25 - 7:28
    although it doesn't really matter
    as long as you're consistent.
  • 7:28 - 7:29
    Then it's going to run all three of those.
  • 7:29 - 7:33
    Is 5, Still 5, Third 5, these lines
    all come out.
  • 7:33 - 7:34
    And then it comes out and prints,
  • 7:36 - 7:39
    and the de-indenting, the fact that this
    print has been moved to line up with
  • 7:39 - 7:42
    the if, that's what indicates that
    this little
  • 7:42 - 7:47
    block of conditional executed code
    is finished.
  • 7:47 - 7:53
    So then prints out Afterwards 5, comes
    down some more, Before 6, then it asks
  • 7:53 - 7:55
    another question, if x is equal to 6,
  • 7:55 - 7:57
    again, that's the question mark
    version of it.
  • 7:58 - 8:01
    And if this is false, now, because x
  • 8:01 - 8:02
    happens to be 5, so the answer
  • 8:02 - 8:06
    to this expression, the logical
    expression, is false.
  • 8:06 - 8:11
    Then it skips all of the indented bits, so
    none of this executes.
  • 8:11 - 8:14
    So, since it's false, it skips all of the
    indented bit, but then it,
  • 8:14 - 8:18
    this print lines up, and so then it picks
    back up with Afterwards 6.
  • 8:18 - 8:21
    So we call this a one-way decision, where
    you have the question, and then
  • 8:21 - 8:24
    you have a couple of things that you're
    going to do on this true, true thing.
  • 8:24 - 8:26
    Or, if it turns out that you're false,
  • 8:26 - 8:28
    you're going to skip all those things.
  • 8:30 - 8:34
    So, Python is actually one of the
  • 8:34 - 8:38
    few languages that uses indentation as
    syntactically significant.
  • 8:40 - 8:43
    We like to indent code to, for ifs, and
  • 8:43 - 8:45
    in a moment, we'll see you
    learn about loops.
  • 8:45 - 8:46
    We like to indent code as a way to
  • 8:47 - 8:51
    make sense of stuff, it makes it
    easier to read.
  • 8:51 - 8:55
    You know, if this thing's inside, and so,
    it, it's really quite nice.
  • 8:55 - 8:55
    And then,
  • 8:55 - 8:57
    we, sort of, use it as a matching, to
  • 8:57 - 9:01
    help us cognitively understand what's
    inside of a program.
  • 9:03 - 9:06
    But in Python, it's really, really
    important, and it's
  • 9:06 - 9:08
    almost, it's, it's, you have to
    think of, like,
  • 9:08 - 9:11
    when you are moving in, you mean
    something,
  • 9:11 - 9:13
    and when you move back out, you mean
    something.
  • 9:13 - 9:16
    So you can increase the indent, which you
    do after, like,
  • 9:16 - 9:18
    an if statement, or any other statement
    that ends in a colon.
  • 9:18 - 9:21
    You increase the indent, and then
  • 9:21 - 9:23
    when you're done, you decrease the indent.
  • 9:23 - 9:25
    You maintain the indent, sort of, for
    sequential code.
  • 9:26 - 9:29
    Now blank lines and comments are ignored.
  • 9:29 - 9:31
    So you can have a blank line and it, it,
    the
  • 9:31 - 9:34
    indentation just goes right past it and
    the comments don't affect it.
  • 9:34 - 9:42
    And so, while we're here, we'll interrupt
    us for a recommendation.
  • 9:44 - 9:50
    In your text editor, Notepad Plus or Text
    Edit or TextWrangler, or whatever
  • 9:50 - 9:55
    you're using, it may be set, when you hit
    the tab key, to move in four spaces.
  • 9:57 - 10:00
    Sometimes you also might move in four
    spaces by hitting space bar four times.
  • 10:01 - 10:04
    Python will see that as different.
  • 10:04 - 10:09
    And it is possible in all of these word
    processors to say, hey,
  • 10:09 - 10:14
    don't actually put tabs in my document,
    when I hit the tab, put in four spaces.
  • 10:14 - 10:18
    Then, whether you're hitting the space bar
    or hitting the tab, at least you
  • 10:18 - 10:19
    are putting the same thing into your
  • 10:19 - 10:21
    document and don't, not freaking
    Python out.
  • 10:23 - 10:26
    If you don't, you may get indentation
    errors.
  • 10:26 - 10:29
    Indentation errors are syntax errors to
    Python.
  • 10:29 - 10:32
    And what's really frustrating is, if you,
    it looks good
  • 10:32 - 10:34
    to you in your text editor,
    you have an if,
  • 10:34 - 10:37
    and the block goes in, and comes back out,
    but one of them is
  • 10:37 - 10:41
    four spaces and one of them is a tab, then
    Python will yell at you.
  • 10:41 - 10:44
    And this is really frustrating, when
    Python yells at you about that.
  • 10:45 - 10:49
    So what I'd like you to do is go into
    your text editor, whatever it is,
  • 10:50 - 10:54
    into the properties or the
    settings.
  • 10:54 - 10:59
    And here is, you know, your, yours may be
    different, but here is where you set this.
  • 11:02 - 11:06
    Auto expand tabs, that is on the Mac in
    TextWrangler, and then,
  • 11:06 - 11:09
    in Notepad Plus Plus, there is replace
  • 11:09 - 11:11
    tabs as spaces, and that's underneath
    preferences.
  • 11:11 - 11:13
    So you have to find it.
  • 11:13 - 11:17
    Stop right now, and go set this so you're
    not going to make yourself crazy.
  • 11:19 - 11:22
    Okay, so, this is kind of a busy slide,
    but it gives
  • 11:22 - 11:28
    you the sense that you have to explicitly
    think about indenting and de-indenting.
  • 11:28 - 11:30
    Okay? And so I'm just going to walk
    through this.
  • 11:30 - 11:33
    So, when you have two lines lining up
  • 11:33 - 11:36
    that means they're going to run
    sequentially.
  • 11:36 - 11:40
    If you see an if, or later here, we'll
    see a for.
  • 11:40 - 11:43
    We haven't talked about for yet, but
    it's, it's like if.
  • 11:43 - 11:46
    So, the fact that we go from
    this second line
  • 11:46 - 11:48
    to this third line and move the indent in,
    we're actually
  • 11:48 - 11:51
    creating a block that has to do
    with this if,
  • 11:51 - 11:53
    and it, you can also kind of tell these,
    the if and
  • 11:53 - 11:56
    the for end in a colon character.
  • 11:56 - 11:59
    Now, we could pull this print back out,
    but we want
  • 11:59 - 12:02
    it to be part of the if, so we maintain
    the indent.
  • 12:02 - 12:05
    And then we're done with the if
    by pulling out.
  • 12:05 - 12:10
    So we line the p with the i, and that
    means this is outside of the if.
  • 12:11 - 12:15
    This for, which we haven't learned about
    for yet, for is another
  • 12:15 - 12:19
    statement that ends in a colon, and
    afterwards you have to indent.
  • 12:19 - 12:22
    Then you maintain the indent.
    Here's an if.
  • 12:22 - 12:25
    But now we have an if, and we're
    already in,
  • 12:25 - 12:27
    but that ends in a colon, so we go in
    farther.
  • 12:29 - 12:30
    And now this is the block.
  • 12:30 - 12:36
    Now, we come back out, and we line up with
    that if, right there, okay?
  • 12:36 - 12:39
    And now, at the end of this, this indent,
    this x here
  • 12:39 - 12:43
    comes all the way back out, so it
    lines up.
  • 12:43 - 12:45
    The rest of these are kind of weird in
    that
  • 12:45 - 12:48
    comments don't matter, blank lines don't
    matter.
  • 12:48 - 12:51
    And so, it just is, sort of, you have to
  • 12:51 - 12:55
    get, mentally get used to the notion that
    these don't count.
  • 12:55 - 12:57
    They can really cognitively mess you up.
  • 12:57 - 13:00
    So these don't count.
  • 13:00 - 13:01
    And now, if I look through it, without,
  • 13:01 - 13:04
    with the comments hidden, it starts in
    column one.
  • 13:05 - 13:10
    Ignore, ignore, goes in, stays in, ignore,
    ignore,
  • 13:10 - 13:14
    ignore, comes out.
    So that's, it all makes sense.
  • 13:14 - 13:18
    Those comments and blank lines are just,
    kind of, confusion.
  • 13:19 - 13:24
    So, increasing and decreasing indent has
    meaning in Python.
  • 13:24 - 13:26
    We'll learn more about this in a bit.
  • 13:26 - 13:29
    Our programs won't get this complex right
    away, but it's important to
  • 13:29 - 13:32
    think, these indents aren't just pretty;
  • 13:32 - 13:35
    they actually are communicating something
    to Python.
  • 13:35 - 13:38
    And what they're communicating is,
    basically, what's in a block.
  • 13:39 - 13:41
    And it shouldn't take you very long, when
    you
  • 13:41 - 13:45
    start looking at Python, to sort of
    visualize these blocks.
  • 13:45 - 13:48
    So, here, here's a big block.
  • 13:48 - 13:51
    This block here, that's got these three
    things.
  • 13:51 - 13:52
    And then, this is a block as well, and
  • 13:52 - 13:55
    you can kind of say, well, here's an if
    statement.
  • 13:55 - 13:57
    And then these are the two statements that
    are part of that if statement.
  • 13:57 - 14:01
    So mentally, you kind of make these block
    pictures.
  • 14:01 - 14:04
    So here's another block.
    This is that for loop.
  • 14:04 - 14:06
    This part's the indented part, but then
    there's a block inside of the block.
  • 14:06 - 14:09
    So you gotta kind of start seeing that as
    well.
  • 14:09 - 14:11
    So this is a block that has to do with
    this green block
  • 14:11 - 14:15
    is the, the one that has to do
    with the if.
  • 14:16 - 14:18
    And then there's a block here, and then
    this is
  • 14:18 - 14:20
    a great big block because this is where it
    finally de-indents.
  • 14:20 - 14:23
    So, don't worry about it yet, but at some
  • 14:23 - 14:27
    point you're just going to start seeing
    this indenting and de-indenting
  • 14:27 - 14:31
    as defining blocks of code
    for various purposes.
  • 14:31 - 14:35
    Now we don't have all the purposes yet,
    but we'll get there.
  • 14:35 - 14:40
    So, we saw in that previous thing one
    block within a block.
  • 14:40 - 14:42
    And, and we're going to do that.
  • 14:42 - 14:44
    We can have ifs, we can have loops that
  • 14:44 - 14:46
    get indented, but then we can indent even
    more.
  • 14:46 - 14:49
    We call that nested, where there's an
    indented
  • 14:49 - 14:51
    area that's in an area that's already
    indented.
  • 14:53 - 14:55
    So here's a nested decision.
  • 14:55 - 14:56
    And it might be easier to start on
  • 14:56 - 14:59
    this side, where I'm going to have
    a first choice.
  • 14:59 - 15:01
    Is x greater than 1, yes or no, and if
  • 15:01 - 15:04
    it's yes, I'll do some work, and
    then I'm going to
  • 15:04 - 15:07
    ask another question, and if that's yes,
    then I'm going to
  • 15:07 - 15:08
    do this, then I'm going to come all the
    way back in.
  • 15:08 - 15:13
    And the way we encode this in Python is, x
    equals 42,
  • 15:13 - 15:18
    if x is greater than 1, it's true, so we
    continue working in the indent.
  • 15:18 - 15:22
    And now we say, oh, if x is less than 100,
    which is still true,
  • 15:22 - 15:26
    so we go in farther, and we do this,
    and now we come out.
  • 15:26 - 15:29
    We don't come out to here, we actually
    keep going
  • 15:29 - 15:32
    all the way to here, so that
    ends both blocks.
  • 15:32 - 15:35
    And so if you sort of think about this,
  • 15:35 - 15:37
    again this is where I want you
    to start seeing
  • 15:37 - 15:40
    what's in a block of code and
    what's not in
  • 15:40 - 15:44
    a block of code, and how the
    indents sort of,
  • 15:44 - 15:46
    like, put a boundary on the
    blocks of code.
  • 15:46 - 15:50
    And so, the first thing you should see is,
    sort of, like, that
  • 15:50 - 15:54
    purple part, the, the x less than 100,
    print, that's kind of a box.
  • 15:54 - 15:58
    And you can see the box on the, on the
    sort of flow diagram as well.
  • 15:58 - 16:00
    The boxes are there. The boxes
    on the flow
  • 16:00 - 16:03
    diagram are places where there's one
    entrance and one exit.
  • 16:09 - 16:11
    And then there's also, sort of, the
    larger box, right?
  • 16:11 - 16:14
    There's this if box that includes the
    smaller box.
  • 16:14 - 16:17
    So, the, there's this nesting, which is
    boxes
  • 16:17 - 16:21
    within boxes, or indented areas within
    indented areas.
  • 16:26 - 16:28
    Now that was a, what we call a one-way
    decision, where
  • 16:28 - 16:31
    you're doing if, and this code either runs
    or it doesn't run.
  • 16:31 - 16:35
    It is extremely common to want to
    basically say,
  • 16:35 - 16:38
    look, I'm going to do one of two things.
    I'm going to
  • 16:38 - 16:40
    ask a question, if the question is true,
    I'm going to do
  • 16:40 - 16:43
    one thing. If the question's false, I'm
    going to do another thing.
  • 16:43 - 16:46
    So that's what we have shown here.
  • 16:46 - 16:49
    We say, is x equals 4, is x equal to
    question mark?
  • 16:49 - 16:51
    If it's yes, we're going to go here.
  • 16:51 - 16:53
    If it's no, we're going to go here.
  • 16:53 - 16:55
    We,re going to execute one or the other, and
    then we're going to continue.
  • 16:56 - 16:58
    So we're really at a fork in the road
    here, right?
  • 16:58 - 17:01
    We're, we're at a fork in the road,
    going to make
  • 17:01 - 17:05
    a choice, and one or the other, but never
    both, right?
  • 17:05 - 17:08
    So, we're going to do one thing, or we're
    going to do another thing.
  • 17:08 - 17:10
    We're going to do one of the two, and
  • 17:10 - 17:12
    depending on what the question
    that we ask,
  • 17:12 - 17:15
    the question that we ask is, which one
    that we're going to do.
  • 17:18 - 17:19
    So here's a little bit of code.
  • 17:22 - 17:26
    x equals 4, is x greater than 2, the
    answer is yes.
  • 17:26 - 17:29
    Then we come out and hit this else and we
    automatically
  • 17:29 - 17:33
    skip, right, because we're only going to
    do one of the two.
  • 17:33 - 17:37
    And here's the picture, x equals 4.
    Is x equal to yes?
  • 17:37 - 17:39
    Print, done.
  • 17:39 - 17:43
    Which means we'll never do both this and
    that, never do both, both sides.
  • 17:43 - 17:46
    We're going to do one or the other of the
    sides.
  • 17:46 - 17:49
    And just sort of
    going with the box,
  • 17:49 - 17:51
    that is our box,
    oops, go back, go back.
  • 17:53 - 17:55
    This is our box, right?
  • 17:55 - 17:58
    It's sort of the indent followed by the
    final indent.
  • 17:58 - 18:00
    The else is really kind of part of it.
  • 18:00 - 18:04
    And then we can draw the picture here.
    It has one entry and one exit.
  • 18:06 - 18:11
    Okay.
    So we have one-way ifs, and we have
  • 18:11 - 18:16
    two-way ifs, and now we have multi-way
    ifs, okay?
  • 18:16 - 18:23
    So, here is a multi-way if,
  • 18:23 - 18:31
    and it introduces a new reserved word, elif,
  • 18:31 - 18:33
    which is a combination of else and if.
  • 18:33 - 18:38
    And this one, probably, is just as easy to
    talk about the picture here.
  • 18:38 - 18:40
    The first question is asked, there's still
  • 18:40 - 18:42
    going to only be one, there's only
    going to
  • 18:42 - 18:46
    be one, one and only one of these three
    choices are going to run.
  • 18:46 - 18:50
    Once it's run one, then it's done, okay?
  • 18:50 - 18:54
    So the, the way to think about this, if x
    is less than 2, we're going to
  • 18:54 - 18:56
    run this one, and then we're going to
    kind of
  • 18:56 - 18:59
    flush all the way out to the bottom.
  • 18:59 - 19:01
    If x is not less than 2, and it's less
    than 10,
  • 19:01 - 19:04
    we're going to run this one, then
    flush out the bottom.
  • 19:04 - 19:06
    And if x is not less than 2, and x is not
  • 19:06 - 19:12
    less than 10, we're going to run this one,
    and flush out the bottom.
  • 19:12 - 19:18
    So, one of these three, one, two, three,
    one of those three is going to run.
  • 19:18 - 19:23
    And it's going to run based on the
    questions that are being asked.
  • 19:23 - 19:24
    The questions do get asked
  • 19:24 - 19:26
    in an order, and the order
    does matter, okay?
  • 19:26 - 19:29
    So that is a multi-way if.
  • 19:29 - 19:35
    If, else if, else.
    So this is kind of like an otherwise.
  • 19:35 - 19:39
    The else is like an otherwise, you know,
    one way or another, we're going to run
  • 19:39 - 19:43
    something, and if none of these first two
    have run, we will run this one.
  • 19:43 - 19:48
    We call it a multi-way if, okay?
  • 19:48 - 19:52
    So, here's an example
    of our multi-way if.
  • 19:52 - 19:55
    That, if we say x equals 0, x equals 0.
  • 19:55 - 19:56
    Is it less than 2?
  • 19:56 - 19:57
    Yes, it is.
  • 19:57 - 20:01
    So we run Small, print Small, and then we
    flush out the bottom.
  • 20:02 - 20:07
    If we switch, instead, x to 5, x is 5.
  • 20:07 - 20:09
    Is it less than 2?
    No, it is not less than 2.
  • 20:09 - 20:10
    Is it less than 10?
  • 20:10 - 20:14
    Well, 5 is less than 10.
    So the answer is yes, so we print Medium,
  • 20:14 - 20:19
    then we flush out the bottom.
    One and only one are going to execute.
  • 20:20 - 20:23
    Now, in this case, we got x is 20.
  • 20:23 - 20:25
    And so we come through here.
    Is it less than 2?
  • 20:25 - 20:27
    No, it is not.
  • 20:27 - 20:29
    Is it less than 10?
    No, it is not.
  • 20:29 - 20:31
    So we're going to do this one, and then
    flush out the bottom.
  • 20:31 - 20:39
    If we go here, it's false, false, go here,
    all else being equal, we run that one.
  • 20:39 - 20:40
    So this one doesn't run
  • 20:40 - 20:44
    and that one doesn't run, right?
    Because these are like gateways.
  • 20:44 - 20:46
    If it were true, it would run it.
  • 20:46 - 20:48
    But it's false, so we're going to skip it.
  • 20:48 - 20:51
    This one, it's false, so we're going to
    skip it.
  • 20:51 - 20:54
    But then we hit the else, that's like a
    catch-all.
  • 20:54 - 20:58
    And then if none of these were true,
    then it will run the else.
  • 20:58 - 21:02
    Any questions?
  • 21:02 - 21:04
    Okay.
    Well,
  • 21:04 - 21:09
    I'm going to ask you a question, in a
    second.
  • 21:12 - 21:15
    Okay, so just a couple of things that
    probably you're wondering about.
  • 21:16 - 21:17
    You don't actually need an else.
  • 21:18 - 21:24
    You can have a multi-way, x equals 5, if x
    is less than 2, there's no else here.
  • 21:24 - 21:26
    You'll notice that this print just comes
    back.
  • 21:26 - 21:30
    And so this way, it could, if both of
    these are false, it could
  • 21:30 - 21:34
    skip them both and just run right through
    here, and there's no else clause, okay?
  • 21:34 - 21:37
    So, in this case, if this one's
  • 21:37 - 21:40
    going to, the way this one's going to run
    is, x equals 5
  • 21:40 - 21:43
    if x is less than 2, it's, it's not.
  • 21:43 - 21:44
    And it skips to here.
  • 21:44 - 21:49
    Else if x is less than 10, which it
    is, it will run that one and come here.
  • 21:49 - 21:55
    But, for a different value of x, like 95,
    boop boop.
  • 21:57 - 22:02
    If x was 95, or 59, this would be false.
    It would skip it.
  • 22:02 - 22:05
    This would, elif, would still be false,
    and it would skip it,
  • 22:05 - 22:08
    and the only thing it would print out
    would be, All done. Okay?
  • 22:08 - 22:13
    Okay, you can also have many elifs.
  • 22:14 - 22:18
    So, better change to green.
  • 22:18 - 22:20
    It checks this one, if it's true, it runs
    the first one.
  • 22:20 - 22:21
    If it's false, it checks this one.
  • 22:21 - 22:25
    If that's true, it run this one, and then
    it skips, right?
  • 22:25 - 22:27
    And so, so the way to think about
  • 22:27 - 22:30
    this is, is, it just goes through and
    checks this one
  • 22:30 - 22:33
    false, this one false, false, false, oh, I
    finally found one.
  • 22:33 - 22:35
    And now I'm done.
  • 22:35 - 22:39
    It still is going to do one and only one
    of these.
  • 22:39 - 22:43
    This one has an else, so that sooner or
    later, it is going to do one.
  • 22:43 - 22:50
    And it only will do the else if all of
    these are false. All have to be false.
  • 22:50 - 22:52
    Then it will, actually, come and hit the
    else clause.
  • 22:54 - 22:55
    It's great, because there are lots of
    situations where
  • 22:55 - 22:58
    you're like, oh, is it before eight in the
    morning?
  • 22:58 - 23:00
    Or is it between eight and noon?
  • 23:00 - 23:02
    Or is it between noon and five?
  • 23:02 - 23:06
    Or after five? After midnight?
    I don't know.
  • 23:06 - 23:11
    Okay, so, here, coming up, is a question.
  • 23:12 - 23:15
    And, you, there's two puzzles
    and I'm going to
  • 23:15 - 23:16
    stop so you can look at them for a while.
  • 23:16 - 23:19
    And I want you to figure out,
  • 23:19 - 23:22
    in both sides of this, which of the lines
  • 23:22 - 23:27
    will not execute, regardless of the value
    for x.
  • 23:27 - 23:29
    So in both sides, there is a line
  • 23:29 - 23:32
    that won't execute, regardless of the
    value for x.
  • 23:32 - 23:33
    Which will never print?
  • 23:33 - 23:38
    There's two problems, problem A and
    problem B.
  • 23:38 - 23:42
    Okay, I'll have some coffee while you
    think.
  • 23:42 - 23:44
    [NOISE].
  • 23:55 - 23:59
    Okay, hopefully you paused it so that you
    could actually think for a bit.
  • 23:59 - 24:01
    So, so I'm going to guess you probably
  • 24:01 - 24:03
    got the first one right, that's pretty
    straightforward.
  • 24:03 - 24:06
    I mean, actually, you're in great shape
    if you got both of them right.
  • 24:06 - 24:09
    If you got any of them right,
    you're in great shape
  • 24:09 - 24:10
    because that means
    you're starting to get it.
  • 24:10 - 24:15
    Starting to like, oh, I'm seeing, kind of,
    this flow picture, there's a picture.
  • 24:15 - 24:17
    I look at these characters that seemingly
  • 24:17 - 24:20
    look like gibberish, and a picture
    arrives.
  • 24:20 - 24:24
    Or a pattern of access execution arises.
  • 24:24 - 24:25
    That's what we want to see.
  • 24:25 - 24:29
    So, the in the first one, which will
    never print?
  • 24:29 - 24:33
    Well, we're looking for kind of a value
    for x which will be defective.
  • 24:33 - 24:35
    So if x is less than 2, we're going
    to do this.
  • 24:35 - 24:39
    Else, if x is greater than or equal to 2,
    we'll do this, else we'll do that.
  • 24:39 - 24:41
    Well, here's the problem with this one.
  • 24:41 - 24:44
    For all values of x, it is, is either going,
  • 24:44 - 24:49
    x is less than 2 is either going to be
    true or greater than equal to 2.
  • 24:49 - 24:52
    Greater than or equal to be, pah,
    for X to be
  • 24:52 - 24:54
    greater than or equal to 2 is
    going to be true.
  • 24:54 - 24:57
    So it's going to run this one, or it's
    going to run that one.
  • 24:57 - 24:58
    So for big numbers, numbers above 2,
  • 24:58 - 25:02
    it's going to run this one;
    below 2, it's going to run that one.
  • 25:02 - 25:05
    So this one is never going to run, okay?
  • 25:05 - 25:07
    Because the, one of the first two is
    going to be
  • 25:07 - 25:11
    true, and so the third else situation
    is not going to run.
  • 25:11 - 25:12
    Hope you got that right.
  • 25:14 - 25:17
    Okay, so let's take a look at the next
    one, okay?
  • 25:17 - 25:20
    So the question is, you know, if x is less
    than 2, do this,
  • 25:20 - 25:23
    if x is less 20, do that, and if x is less
    than 10, do this,
  • 25:23 - 25:29
    and otherwise do that.
    Well, the one that will never execute
  • 25:29 - 25:34
    is this one. And, x equals 15,
  • 25:34 - 25:39
    no, x equals 15 is a bad one,
    x equals 5 is the one that will,
  • 25:39 - 25:44
    sort of, cause it to behave badly.
  • 25:44 - 25:49
    And so, if x is 5, this is false.
  • 25:49 - 25:53
    If x is less than 20, this is true, and
    then it's done.
  • 25:53 - 25:59
    So the problem is, this is the one that
    will never execute, because
  • 25:59 - 26:04
    if a value is less than 10, it's also
    less than 20, so this will be true.
  • 26:04 - 26:07
    So for a value like 5, which happens to be
    less than 10,
  • 26:07 - 26:10
    which you would think would cause that
    line to execute, does not.
  • 26:12 - 26:14
    This one executes because it's checked
    first.
  • 26:14 - 26:17
    Now, if we just moved this code, took this
    code and
  • 26:17 - 26:21
    we moved it down here, then it would make
    more sense, okay?
  • 26:21 - 26:22
    Oops.
  • 26:22 - 26:24
    If we moved it down there, it would make
    more sense.
  • 26:24 - 26:30
    But basically, the answer to these is,
    change color, this one won't
  • 26:30 - 26:36
    execute, and this one will never execute
    for any value, so there's the answer.
  • 26:38 - 26:40
    Okay, so we're almost done with
    conditionals.
  • 26:40 - 26:44
    I want to show you one more kind of
    conditional.
  • 26:44 - 26:45
    It's a little bit different.
  • 26:46 - 26:51
    It's not a bit of code structure
    that you make, it is,
  • 26:51 - 26:57
    it is dealing with the fact that some
    things may blow up.
  • 26:57 - 27:00
    Like, if you read a number from a user and
    you try to convert it
  • 27:00 - 27:03
    to a floating point number, as you may
  • 27:03 - 27:06
    have already done in some of your
    homework,
  • 27:06 - 27:07
    it can blow up.
  • 27:07 - 27:12
    You know it's going to blow up, but you
    don't exactly want it to kill your program.
  • 27:12 - 27:17
    So, the concept of try and except are,
    hey, this is a dangerous thing.
  • 27:17 - 27:18
    I know it might blow up.
  • 27:18 - 27:21
    I know exactly what it might blow up, but
    I don't want to die
  • 27:21 - 27:23
    I don't want to stop my program
    when it blows up.
  • 27:23 - 27:24
    I want to continue.
  • 27:24 - 27:27
    And that's the purpose of the except
    block.
  • 27:27 - 27:29
    So, here's a little bit of code.
  • 27:29 - 27:31
    And, you know, it's, we've done
    this code before.
  • 27:31 - 27:34
    This is code that's kind of
    similar to, like
  • 27:34 - 27:37
    your rate and pay homework, where
    you read a string
  • 27:37 - 27:41
    using raw input, you converted it using
    float, but
  • 27:41 - 27:44
    then if you typed in Fred, the
    thing blows up.
  • 27:44 - 27:46
    So we're kind of simulating that
    right here.
  • 27:46 - 27:49
    So here we have a variable astr
    called Hello Bob,
  • 27:49 - 27:52
    and then we try to turn it into an
    integer.
  • 27:52 - 27:53
    And then we're going to print that out,
    and then we have
  • 27:53 - 27:57
    another string called one, and that has
    the letters 1, 2, 3.
  • 27:57 - 28:01
    We convert that to an integer, and then we
    print that one out.
  • 28:01 - 28:04
    The problem is, is that when this runs,
  • 28:06 - 28:07
    this is going to fail.
  • 28:07 - 28:10
    It's going to fail with this traceback,
    okay?
  • 28:10 - 28:17
    And the problem is, is when the traceback
    happens, the program stops executing.
  • 28:17 - 28:22
    The traceback is Python's way of asking
    you, hey, this
  • 28:22 - 28:25
    would be bad, I don't know what to do,
    I'm stopping.
  • 28:25 - 28:30
    So that means that the rest of your
    program is gone, right?
  • 28:30 - 28:31
    It,
  • 28:31 - 28:34
    The fact that we had stuff down here
    doesn't matter.
  • 28:34 - 28:37
    This line died with the traceback.
  • 28:37 - 28:38
    It stopped.
  • 28:38 - 28:41
    It doesn't, like, give you a traceback and
    then keep going.
  • 28:41 - 28:44
    It gives you a traceback, and
    that's the end.
  • 28:44 - 28:45
    Now this might be something, instead of
  • 28:45 - 28:48
    just the string, Hello Bob, which is
    insane.
  • 28:48 - 28:52
    Data might have come from a raw input,
    where the user was typing, and you say,
  • 28:52 - 28:53
    give me a number, and they type something
  • 28:53 - 28:56
    that's not a number, and this would
    blow up.
  • 28:56 - 28:57
    It's like, hey, I know it's going to
    blow up.
  • 28:58 - 29:04
    The problem with this is that you don't,
    oops, erp, clear the thing.
  • 29:04 - 29:06
    Oh and now we have to start it on fire again.
  • 29:06 - 29:08
    Okay, it's on fire.
  • 29:08 - 29:12
    The problem is, is that in a sense, this
    program is you.
  • 29:13 - 29:17
    If you recall, we have you as the, typing
    these commands
  • 29:17 - 29:21
    into these scripts, feeding the central
    processing unit, answering the question
  • 29:21 - 29:22
    what next?
  • 29:22 - 29:28
    So you should take it a little personally
    when your program gets a traceback.
  • 29:28 - 29:32
    because that means you, in the form of
    your program, have been vaporized.
  • 29:32 - 29:36
    And you're not present to give any more
    instructions.
  • 29:36 - 29:37
    It stops.
  • 29:37 - 29:40
    It stops dead in its tracks.
    You are gone.
  • 29:40 - 29:44
    So, we want to make sure we control this
    behavior.
  • 29:44 - 29:47
    We know it might blow up,
  • 29:47 - 29:53
    and we want to capture the situation where
    it does, and execute alternate code.
  • 29:53 - 29:54
    Okay.
  • 29:54 - 29:55
    So here it goes.
  • 29:55 - 29:57
    It's a bit of syntax.
  • 29:57 - 30:00
    I mentioned that it uses the try and
    except keywords.
  • 30:00 - 30:05
    These are reserved words in Python.
    And then it's a little indented block.
  • 30:05 - 30:08
    So, astr equals Hello Bob, great.
  • 30:08 - 30:12
    Try means, we're about to do something
    dangerous, let's take out some
  • 30:12 - 30:13
    insurance policy on it.
  • 30:13 - 30:16
    And that is, we're going to convert this
    to an integer.
  • 30:16 - 30:20
    Take astr, convert it to an integer, put
    it in istr.
  • 30:21 - 30:24
    If that works, great, we'll just continue
    on, and ignore this except.
  • 30:24 - 30:27
    If it blows up, we're going to jump into
    the
  • 30:27 - 30:31
    except block, and then we'll have
    alternate substitute code.
  • 30:31 - 30:35
    In this case, I'm going to set the
    variable to negative 1 as an indicator.
  • 30:35 - 30:38
    Then I'll print it out, and I'll do it
    again.
  • 30:38 - 30:41
    Try this code, and away we go.
  • 30:41 - 30:46
    So, when this runs, we know exactly how
    it's going to run.
  • 30:46 - 30:52
    It, whoa, woop, come back.
  • 30:52 - 30:55
    We'll set this string, the try
    takes out the insurance,
  • 30:55 - 31:02
    this blows up, so it runs down to here
    and runs this part, and then it'll
  • 31:02 - 31:07
    print First minus 1.
    And it sets the string to 1, 2, 3, not
  • 31:07 - 31:12
    123, but 1, 2, 3 as a string.
    It takes out an insurance policy.
  • 31:12 - 31:18
    This time it works, and that puts
    istr is going to be 123,
  • 31:18 - 31:22
    so we don't run the accept code, and
    so out comes the
  • 31:22 - 31:28
    second 1, 2, 3, okay?
    So the try is, take out insurance on this
  • 31:28 - 31:31
    little bit of code, and if it fails,
  • 31:31 - 31:36
    run this alternate code. If not, skip the
    alternate code.
  • 31:36 - 31:37
    So it's kind of conditional.
  • 31:37 - 31:40
    If you put multiple lines in the block
    between
  • 31:40 - 31:45
    the try and the except, it runs until one
    dies.
  • 31:45 - 31:46
    So it doesn't come back, okay?
  • 31:46 - 31:50
    It's not taking insurance out on,
    separately, on all three statements.
  • 31:50 - 31:53
    It's like, here's a block of stuff, and if
    anything blows up,
  • 31:53 - 31:57
    stop.
    And the things that run do run.
  • 31:57 - 31:59
    So if, this is really kind of bad code,
  • 31:59 - 32:02
    because you really don't want
    the print in here.
  • 32:02 - 32:05
    It's actually a good idea on the try
    except to have as little in the
  • 32:05 - 32:09
    try block as you possibly can, so you're
    real clear on what's going to fail.
  • 32:12 - 32:16
    but, so here we come in, shh, it's Bob,
    so it's going to fail.
  • 32:16 - 32:16
    We run this.
  • 32:16 - 32:18
    That runs successfully.
  • 32:18 - 32:24
    This blows up, so it quits and jumps into
    the except blocks and continues.
  • 32:24 - 32:28
    The point is, is that this code never
    executes, never executes.
  • 32:28 - 32:31
    The other point is, this code does
    execute.
  • 32:31 - 32:34
    Just because this blew up, this is already
    executed,
  • 32:34 - 32:37
    it might have done something other, more
    complex than
  • 32:37 - 32:40
    print Hello, okay?
    So, so there you go.
  • 32:40 - 32:42
    So, if we look at this kind of in a
    picture,
  • 32:42 - 32:45
    we, we set this through the try block, it
    runs, it runs.
  • 32:45 - 32:51
    And the, the try except kind of has this
    escape hatch that says, if there is
  • 32:51 - 32:55
    a [SOUND] explosion somehow, then
    it runs this
  • 32:55 - 33:00
    alternate code and then it comes out and
    finishes, okay?
  • 33:00 - 33:02
    And, again, this, it doesn't go
  • 33:02 - 33:07
    back and finish the block, and it doesn't
    undo the work that is done by that.
  • 33:07 - 33:09
    So it doesn't un-execute it.
  • 33:09 - 33:12
    If it executes and works, it just keeps on
    going, then
  • 33:12 - 33:17
    it blows up, and then sort of flushes its
    way out, okay?
  • 33:17 - 33:21
    So here's an example of how you might do
    this in a running program, like the
  • 33:21 - 33:23
    programs that you're about to be assigned,
    where
  • 33:23 - 33:26
    you're supposed to check for user input
    having errors.
  • 33:27 - 33:33
    So, here is a little conversion of a
    number, and
  • 33:33 - 33:38
    and so we're saying, you know, enter a number,
    and we're putting a string into rawstr.
  • 33:38 - 33:42
    It's a string, and and so, we don't know.
  • 33:42 - 33:44
    And here's where we're going to convert it
    into an integer,
  • 33:44 - 33:46
    and we're just not sure if it's going to
    work or not.
  • 33:46 - 33:52
    So, we know how int works. It either
    converts it or it blows up.
  • 33:52 - 33:52
    So we know
  • 33:52 - 33:53
    it's going to do that, we just don't
  • 33:53 - 33:56
    know what the user's going to type, we
    don't know.
  • 33:56 - 33:58
    So we have to take out insurance on it.
    So this runs,
  • 33:58 - 34:00
    and then we do a try, and then we
    try to convert it,
  • 34:00 - 34:04
    and if it works, it's great, and if
  • 34:04 - 34:07
    it fails, it runs this and sets it to
    negative 1.
  • 34:07 - 34:12
    And afterwards, we either have the number
    or negative 1.
  • 34:12 - 34:16
    And so, if the person enters 42,
    it says Nice work.
  • 34:17 - 34:19
    Well, let's show you where it runs.
  • 34:19 - 34:25
    If the person says 42, it runs through
    here, gets the string 42, converts that
  • 34:25 - 34:30
    to an integer, skips here, and then says,
    Nice work, and that's how it runs.
  • 34:30 - 34:34
    If, on the other hand, they type
    forty two, the words,
  • 34:34 - 34:37
    this gets to be the string forty two.
  • 34:37 - 34:40
    It runs here, this blows up.
  • 34:40 - 34:42
    So it comes and runs this part here.
  • 34:42 - 34:47
    And then it says, if ival is greater than
    0, which is not true,
  • 34:47 - 34:51
    so it runs this part and says
    Not a number.
  • 34:51 - 34:54
    So this is our way of compensating for
    user input
  • 34:54 - 34:59
    that might have errors in it, errors that
    we're anticipating, right?
  • 34:59 - 35:02
    You'd, you'd rather at least
    put up some kind of a
  • 35:02 - 35:05
    message, rather than just have a
    traceback,
  • 35:05 - 35:07
    if you're writing code for somebody else.
  • 35:07 - 35:08
    It just kind of is
  • 35:08 - 35:13
    not very classy. So,
  • 35:13 - 35:20
    the classic program to do this is a
    time-and-a-half for overtime pay.
  • 35:23 - 35:26
    So you get some pay rate like
    $10 an hour for your first 40 hours,
  • 35:26 - 35:30
    and then you get 15 hours, for
    any hours above it.
  • 35:30 - 35:34
    So you have to sort of say, oh, okay, if
    this ends up being, this ends up being
  • 35:34 - 35:40
    some kind of a thing, where, let me draw
    that picture a little better.
  • 35:40 - 35:44
    Hours greater than 40, you're going to do
    one thing, and
  • 35:44 - 35:48
    if hours are less than 40, you're going to
    do another thing.
  • 35:48 - 35:50
    So you have two payout calculations.
  • 35:50 - 35:54
    If the hours are greater than 40, then
    you're going to do
  • 35:54 - 35:59
    a overtime calculation, which is kind of
    like, 40 times the regular rate.
  • 35:59 - 36:02
    And then, the number of excess hours,
    like 5 overtime hours
  • 36:02 - 36:06
    times the reg, rate times
    one-and-a-half.
  • 36:06 - 36:11
    So this is kind of the calculation that
    happens if the hours are greater than 40.
  • 36:11 - 36:16
    And then, if the hours are less than 40,
    it's just pay
  • 36:16 - 36:19
    equals rate times hours.
  • 36:19 - 36:24
    So it, you're going to do one of two
    calculations, depending on how it works.
  • 36:24 - 36:28
    So that's one of the programming problems
    for this chapter.
  • 36:28 - 36:29
    That's a classic.
  • 36:29 - 36:31
    It's the classic if, then, else.
  • 36:31 - 36:33
    You can actually do it with an if, then
    if you're tricky.
  • 36:33 - 36:36
    There's a lot of ways to do this, so pick
    a, pick one and do it.
  • 36:36 - 36:41
    Now the next thing I want you to do is, I
    want you to take that
  • 36:41 - 36:48
    same program, do it again in another,
    another assignment, or another problem in
  • 36:48 - 36:54
    the chapter, and that is have some kind of
    a non-numeric input, and have it blow up.
  • 36:54 - 37:00
    So if they type, you know, something like
    nine, put out an error.
  • 37:00 - 37:03
    Or, if they type something here, put out
    an error.
  • 37:03 - 37:06
    Now, don't write a loop.
    No loop.
  • 37:07 - 37:11
    This is one execution of the program, and
    this is another execution of the program.
  • 37:11 - 37:12
    Later, we can write loops.
  • 37:12 - 37:15
    We haven't even talked about loops.
    So this is running it twice.
  • 37:15 - 37:18
    All I want you to do is exit.
  • 37:18 - 37:20
    So take a look in the book as to how to
    just get out.
  • 37:20 - 37:23
    So it, it's, it, I don't want you to try
    to say, I'm going to
  • 37:23 - 37:27
    prompt for these numbers until I get a
    good one, we'll do that later.
  • 37:27 - 37:29
    I just want you to deal with the
    fact that
  • 37:29 - 37:32
    you read a thing, you use the try
  • 37:32 - 37:35
    to convert it to a float and see if it
    works.
  • 37:35 - 37:36
    And if you don't, you just quit.
  • 37:36 - 37:40
    Don't, don't, don't try to be tricky and
    repeatedly prompt.
  • 37:40 - 37:47
    So don't repeatedly prompt.
    One prompt, and then
  • 37:47 - 37:52
    quit, okay?
    So, this is contintous-,
  • 37:52 - 37:57
    conditional execution, if, if then else,
    and then I added a little bit with
  • 37:57 - 38:00
    the try and except, as well.
  • 38:00 - 38:04
    And the try and except is really a
    limited kind of a problem.
  • 38:04 - 38:09
    It really is to compensate for errors that
    you anticipate are going to happen, and
  • 38:09 - 38:11
    you can imagine what you want to do
  • 38:11 - 38:15
    as a replacement for what those
    errors are, okay?
  • 38:15 - 38:15
    See you next lecture.
Title:
Python for Informatics - Chapter 3 - Conditional Execution
Description:

This explores various kinds of conditional execution statements like if, else, elif, and try / except code. We also look at indentation and how indentation is used in Python programs. This is provided by www.pythonlearn.com
All Lectures: http://www.youtube.com/playlist?list=PLlRFEj9H3Oj4JXIwMwN1_ss1Tk8wZShEJ

more » « less
Video Language:
English
Team:
Captions Requested
Duration:
38:16

English subtitles

Revisions