0 00:00:01,100 --> 00:00:05,910 Okay, so we're almost done with conditionals. 1 00:00:05,910 --> 00:00:09,740 I want to show you one more kind of conditional. 2 00:00:09,740 --> 00:00:10,780 It's a little bit different. 3 00:00:12,110 --> 00:00:17,308 It's not a bit of code structure that you make, it is, 4 00:00:17,308 --> 00:00:22,621 it is dealing with the fact that some things may blow up. 5 00:00:22,621 --> 00:00:26,395 Like, if you read a number from a user and you try to convert it 6 00:00:26,395 --> 00:00:28,763 to a floating point number, as you may 7 00:00:28,763 --> 00:00:31,797 have already done in some of your homework, 8 00:00:31,797 --> 00:00:32,764 it can blow up. 9 00:00:32,764 --> 00:00:38,060 You know it's going to blow up, but you don't exactly want it to kill your program. 10 00:00:38,060 --> 00:00:43,430 So, the concept of try and except are, hey, this is a dangerous thing. 11 00:00:43,430 --> 00:00:44,430 I know it might blow up. 12 00:00:44,430 --> 00:00:46,531 I know exactly what it might blow up, but I don't want to die 13 00:00:46,531 --> 00:00:48,990 I don't want to stop my program when it blows up. 14 00:00:48,990 --> 00:00:50,260 I want to continue. 15 00:00:50,260 --> 00:00:52,690 And that's the purpose of the except block. 16 00:00:52,690 --> 00:00:54,520 So, here's a little bit of code. 17 00:00:54,520 --> 00:00:57,210 And, you know, it's, we've done this code before. 18 00:00:57,210 --> 00:00:59,880 This is code that's kind of similar to, like 19 00:00:59,880 --> 00:01:02,850 your rate and pay homework, where you read a string 20 00:01:02,850 --> 00:01:06,710 using raw input, you converted it using float, but 21 00:01:06,710 --> 00:01:09,950 then if you typed in Fred, the thing blows up. 22 00:01:09,950 --> 00:01:12,190 So we're kind of simulating that right here. 23 00:01:12,190 --> 00:01:15,080 So here we have a variable astr called Hello Bob, 24 00:01:15,080 --> 00:01:17,580 and then we try to turn it into an integer. 25 00:01:17,580 --> 00:01:19,370 And then we're going to print that out, and then we have 26 00:01:19,370 --> 00:01:22,920 another string called one, and that has the letters 1, 2, 3. 27 00:01:22,920 --> 00:01:26,580 We convert that to an integer, and then we print that one out. 28 00:01:26,580 --> 00:01:29,600 The problem is, is that when this runs, 29 00:01:31,700 --> 00:01:33,020 this is going to fail. 30 00:01:33,020 --> 00:01:36,450 It's going to fail with this traceback, okay? 31 00:01:36,450 --> 00:01:43,240 And the problem is, is when the traceback happens, the program stops executing. 32 00:01:43,240 --> 00:01:48,330 The traceback is Python's way of asking you, hey, this 33 00:01:48,330 --> 00:01:51,330 would be bad, I don't know what to do, I'm stopping. 34 00:01:51,330 --> 00:01:56,478 So that means that the rest of your program is gone, right? 35 00:01:56,478 --> 00:01:56,682 It, 36 00:01:56,682 --> 00:01:59,700 The fact that we had stuff down here doesn't matter. 37 00:01:59,700 --> 00:02:02,700 This line died with the traceback. 38 00:02:02,700 --> 00:02:03,790 It stopped. 39 00:02:03,790 --> 00:02:06,550 It doesn't, like, give you a traceback and then keep going. 40 00:02:06,550 --> 00:02:09,661 It gives you a traceback, and that's the end. 41 00:02:09,661 --> 00:02:11,293 Now this might be something, instead of 42 00:02:11,293 --> 00:02:14,060 just the string, Hello Bob, which is insane. 43 00:02:14,060 --> 00:02:17,530 Data might have come from a raw input, where the user was typing, and you say, 44 00:02:17,530 --> 00:02:19,450 give me a number, and they type something 45 00:02:19,450 --> 00:02:21,650 that's not a number, and this would blow up. 46 00:02:21,650 --> 00:02:23,210 It's like, hey, I know it's going to blow up. 47 00:02:24,270 --> 00:02:29,613 The problem with this is that you don't, oops, erp, clear the thing. 48 00:02:29,613 --> 00:02:32,403 Oh and now we have to start it on fire again. 49 00:02:32,403 --> 00:02:33,964 Okay, it's on fire. 50 00:02:33,964 --> 00:02:37,550 The problem is, is that in a sense, this program is you. 51 00:02:38,570 --> 00:02:42,970 If you recall, we have you as the, typing these commands 52 00:02:42,970 --> 00:02:47,010 into these scripts, feeding the central processing unit, answering the question 53 00:02:47,010 --> 00:02:48,430 what next? 54 00:02:48,430 --> 00:02:54,320 So you should take it a little personally when your program gets a traceback. 55 00:02:54,320 --> 00:02:58,180 because that means you, in the form of your program, have been vaporized. 56 00:02:58,180 --> 00:03:01,870 And you're not present to give any more instructions. 57 00:03:01,870 --> 00:03:02,620 It stops. 58 00:03:02,620 --> 00:03:06,450 It stops dead in its tracks. You are gone. 59 00:03:06,450 --> 00:03:10,350 So, we want to make sure we control this behavior. 60 00:03:10,350 --> 00:03:12,790 We know it might blow up, 61 00:03:12,790 --> 00:03:18,600 and we want to capture the situation where it does, and execute alternate code. 62 00:03:18,600 --> 00:03:19,580 Okay. 63 00:03:19,580 --> 00:03:21,120 So here it goes. 64 00:03:21,120 --> 00:03:22,780 It's a bit of syntax. 65 00:03:22,780 --> 00:03:26,230 I mentioned that it uses the try and except keywords. 66 00:03:26,230 --> 00:03:30,560 These are reserved words in Python. And then it's a little indented block. 67 00:03:30,560 --> 00:03:34,390 So, astr equals Hello Bob, great. 68 00:03:34,390 --> 00:03:38,010 Try means, we're about to do something dangerous, let's take out some 69 00:03:38,010 --> 00:03:39,490 insurance policy on it. 70 00:03:39,490 --> 00:03:42,420 And that is, we're going to convert this to an integer. 71 00:03:42,420 --> 00:03:45,830 Take astr, convert it to an integer, put it in istr. 72 00:03:46,860 --> 00:03:50,430 If that works, great, we'll just continue on, and ignore this except. 73 00:03:50,430 --> 00:03:53,415 If it blows up, we're going to jump into the 74 00:03:53,415 --> 00:03:57,220 except block, and then we'll have alternate substitute code. 75 00:03:57,220 --> 00:04:01,330 In this case, I'm going to set the variable to negative 1 as an indicator. 76 00:04:01,330 --> 00:04:03,800 Then I'll print it out, and I'll do it again. 77 00:04:03,800 --> 00:04:06,860 Try this code, and away we go. 78 00:04:06,860 --> 00:04:11,578 So, when this runs, we know exactly how it's going to run. 79 00:04:11,578 --> 00:04:17,516 It, whoa, woop, come back. 80 00:04:17,516 --> 00:04:21,284 We'll set this string, the try takes out the insurance, 81 00:04:21,284 --> 00:04:27,846 this blows up, so it runs down to here and runs this part, and then it'll 82 00:04:27,846 --> 00:04:33,300 print First minus 1. And it sets the string to 1, 2, 3, not 83 00:04:33,300 --> 00:04:37,788 123, but 1, 2, 3 as a string. It takes out an insurance policy. 84 00:04:37,788 --> 00:04:43,835 This time it works, and that puts istr is going to be 123, 85 00:04:43,835 --> 00:04:48,191 so we don't run the accept code, and so out comes the 86 00:04:48,191 --> 00:04:53,969 second 1, 2, 3, okay? So the try is, take out insurance on this 87 00:04:53,969 --> 00:04:57,097 little bit of code, and if it fails, 88 00:04:57,097 --> 00:05:01,563 run this alternate code. If not, skip the alternate code. 89 00:05:01,563 --> 00:05:03,079 So it's kind of conditional. 90 00:05:03,079 --> 00:05:06,499 If you put multiple lines in the block between 91 00:05:06,499 --> 00:05:10,530 the try and the except, it runs until one dies. 92 00:05:10,530 --> 00:05:12,080 So it doesn't come back, okay? 93 00:05:12,080 --> 00:05:16,430 It's not taking insurance out on, separately, on all three statements. 94 00:05:16,430 --> 00:05:18,690 It's like, here's a block of stuff, and if anything blows up, 95 00:05:18,690 --> 00:05:22,870 stop. And the things that run do run. 96 00:05:22,870 --> 00:05:25,078 So if, this is really kind of bad code, 97 00:05:25,078 --> 00:05:28,010 because you really don't want the print in here. 98 00:05:28,010 --> 00:05:31,350 It's actually a good idea on the try except to have as little in the 99 00:05:31,350 --> 00:05:35,300 try block as you possibly can, so you're real clear on what's going to fail. 100 00:05:37,580 --> 00:05:41,530 but, so here we come in, shh, it's Bob, so it's going to fail. 101 00:05:41,530 --> 00:05:42,400 We run this. 102 00:05:42,400 --> 00:05:44,170 That runs successfully. 103 00:05:44,170 --> 00:05:49,540 This blows up, so it quits and jumps into the except blocks and continues. 104 00:05:49,540 --> 00:05:54,231 The point is, is that this code never executes, never executes. 105 00:05:54,231 --> 00:05:57,380 The other point is, this code does execute. 106 00:05:57,380 --> 00:06:00,094 Just because this blew up, this is already executed, 107 00:06:00,094 --> 00:06:02,808 it might have done something other, more complex than 108 00:06:02,808 --> 00:06:06,280 print Hello, okay? So, so there you go. 109 00:06:06,280 --> 00:06:08,410 So, if we look at this kind of in a picture, 110 00:06:08,410 --> 00:06:11,494 we, we set this through the try block, it runs, it runs. 111 00:06:11,494 --> 00:06:17,479 And the, the try except kind of has this escape hatch that says, if there is 112 00:06:17,479 --> 00:06:21,089 a [SOUND] explosion somehow, then it runs this 113 00:06:21,089 --> 00:06:25,622 alternate code and then it comes out and finishes, okay? 114 00:06:25,622 --> 00:06:27,872 And, again, this, it doesn't go 115 00:06:27,872 --> 00:06:32,767 back and finish the block, and it doesn't undo the work that is done by that. 116 00:06:32,767 --> 00:06:35,170 So it doesn't un-execute it. 117 00:06:35,170 --> 00:06:37,850 If it executes and works, it just keeps on going, then 118 00:06:37,850 --> 00:06:42,790 it blows up, and then sort of flushes its way out, okay? 119 00:06:42,790 --> 00:06:47,130 So here's an example of how you might do this in a running program, like the 120 00:06:47,130 --> 00:06:48,980 programs that you're about to be assigned, where 121 00:06:48,980 --> 00:06:51,710 you're supposed to check for user input having errors. 122 00:06:52,920 --> 00:06:59,020 So, here is a little conversion of a number, and 123 00:06:59,020 --> 00:07:04,430 and so we're saying, you know, enter a number, and we're putting a string into rawstr. 124 00:07:04,430 --> 00:07:08,260 It's a string, and and so, we don't know. 125 00:07:08,260 --> 00:07:10,460 And here's where we're going to convert it into an integer, 126 00:07:10,460 --> 00:07:12,348 and we're just not sure if it's going to work or not. 127 00:07:12,348 --> 00:07:17,758 So, we know how int works. It either converts it or it blows up. 128 00:07:17,758 --> 00:07:17,950 So we know 129 00:07:17,950 --> 00:07:19,486 it's going to do that, we just don't 130 00:07:19,486 --> 00:07:21,627 know what the user's going to type, we don't know. 131 00:07:21,627 --> 00:07:24,439 So we have to take out insurance on it. So this runs, 132 00:07:24,439 --> 00:07:26,029 and then we do a try, and then we try to convert it, 133 00:07:26,029 --> 00:07:30,137 and if it works, it's great, and if 134 00:07:30,137 --> 00:07:33,370 it fails, it runs this and sets it to negative 1. 135 00:07:33,370 --> 00:07:37,860 And afterwards, we either have the number or negative 1. 136 00:07:37,860 --> 00:07:42,200 And so, if the person enters 42, it says Nice work. 137 00:07:43,200 --> 00:07:45,440 Well, let's show you where it runs. 138 00:07:45,440 --> 00:07:50,664 If the person says 42, it runs through here, gets the string 42, converts that 139 00:07:50,664 --> 00:07:55,650 to an integer, skips here, and then says, Nice work, and that's how it runs. 140 00:07:55,650 --> 00:07:59,614 If, on the other hand, they type forty two, the words, 141 00:07:59,614 --> 00:08:02,820 this gets to be the string forty two. 142 00:08:02,820 --> 00:08:05,940 It runs here, this blows up. 143 00:08:05,940 --> 00:08:08,290 So it comes and runs this part here. 144 00:08:08,290 --> 00:08:12,520 And then it says, if ival is greater than 0, which is not true, 145 00:08:12,520 --> 00:08:16,510 so it runs this part and says Not a number. 146 00:08:16,510 --> 00:08:20,380 So this is our way of compensating for user input 147 00:08:20,380 --> 00:08:25,300 that might have errors in it, errors that we're anticipating, right? 148 00:08:25,300 --> 00:08:28,210 You'd, you'd rather at least put up some kind of a 149 00:08:28,210 --> 00:08:31,060 message, rather than just have a traceback, 150 00:08:31,060 --> 00:08:32,800 if you're writing code for somebody else. 151 00:08:32,800 --> 00:08:34,080 It just kind of is 152 00:08:34,080 --> 00:08:38,719 not very classy. So, 153 00:08:38,719 --> 00:08:46,490 the classic program to do this is a time-and-a-half for overtime pay. 154 00:08:49,240 --> 00:08:52,376 So you get some pay rate like $10 an hour for your first 40 hours, 155 00:08:52,376 --> 00:08:55,951 and then you get 15 hours, for any hours above it. 156 00:08:55,951 --> 00:09:00,297 So you have to sort of say, oh, okay, if this ends up being, this ends up being 157 00:09:00,297 --> 00:09:06,273 some kind of a thing, where, let me draw that picture a little better. 158 00:09:06,273 --> 00:09:09,654 Hours greater than 40, you're going to do one thing, and 159 00:09:09,654 --> 00:09:13,570 if hours are less than 40, you're going to do another thing. 160 00:09:13,570 --> 00:09:15,800 So you have two payout calculations. 161 00:09:15,800 --> 00:09:19,725 If the hours are greater than 40, then you're going to do 162 00:09:19,725 --> 00:09:24,900 a overtime calculation, which is kind of like, 40 times the regular rate. 163 00:09:24,900 --> 00:09:27,910 And then, the number of excess hours, like 5 overtime hours 164 00:09:27,910 --> 00:09:31,620 times the reg, rate times one-and-a-half. 165 00:09:31,620 --> 00:09:36,560 So this is kind of the calculation that happens if the hours are greater than 40. 166 00:09:36,560 --> 00:09:41,960 And then, if the hours are less than 40, it's just pay 167 00:09:41,960 --> 00:09:44,690 equals rate times hours. 168 00:09:44,690 --> 00:09:49,660 So it, you're going to do one of two calculations, depending on how it works. 169 00:09:49,660 --> 00:09:53,720 So that's one of the programming problems for this chapter. 170 00:09:53,720 --> 00:09:55,330 That's a classic. 171 00:09:55,330 --> 00:09:57,240 It's the classic if, then, else. 172 00:09:57,240 --> 00:09:59,080 You can actually do it with an if, then if you're tricky. 173 00:09:59,080 --> 00:10:02,462 There's a lot of ways to do this, so pick a, pick one and do it. 174 00:10:02,462 --> 00:10:07,247 Now the next thing I want you to do is, I want you to take that 175 00:10:07,247 --> 00:10:13,685 same program, do it again in another, another assignment, or another problem in 176 00:10:13,685 --> 00:10:20,430 the chapter, and that is have some kind of a non-numeric input, and have it blow up. 177 00:10:20,430 --> 00:10:26,060 So if they type, you know, something like nine, put out an error. 178 00:10:26,060 --> 00:10:28,510 Or, if they type something here, put out an error. 179 00:10:28,510 --> 00:10:32,070 Now, don't write a loop. No loop. 180 00:10:33,110 --> 00:10:37,240 This is one execution of the program, and this is another execution of the program. 181 00:10:37,240 --> 00:10:38,160 Later, we can write loops. 182 00:10:38,160 --> 00:10:41,240 We haven't even talked about loops. So this is running it twice. 183 00:10:41,240 --> 00:10:43,750 All I want you to do is exit. 184 00:10:43,750 --> 00:10:46,290 So take a look in the book as to how to just get out. 185 00:10:46,290 --> 00:10:49,080 So it, it's, it, I don't want you to try to say, I'm going to 186 00:10:49,080 --> 00:10:52,900 prompt for these numbers until I get a good one, we'll do that later. 187 00:10:52,900 --> 00:10:54,910 I just want you to deal with the fact that 188 00:10:54,910 --> 00:10:58,390 you read a thing, you use the try 189 00:10:58,390 --> 00:11:00,900 to convert it to a float and see if it works. 190 00:11:00,900 --> 00:11:02,160 And if you don't, you just quit. 191 00:11:02,160 --> 00:11:06,500 Don't, don't, don't try to be tricky and repeatedly prompt. 192 00:11:06,500 --> 00:11:12,554 So don't repeatedly prompt. One prompt, and then 193 00:11:12,554 --> 00:11:17,870 quit, okay? So, this is contintous-, 194 00:11:17,870 --> 00:11:23,400 conditional execution, if, if then else, and then I added a little bit with 195 00:11:23,400 --> 00:11:25,810 the try and except, as well. 196 00:11:25,810 --> 00:11:29,869 And the try and except is really a limited kind of a problem. 197 00:11:29,869 --> 00:11:35,113 It really is to compensate for errors that you anticipate are going to happen, and 198 00:11:35,113 --> 00:11:37,317 you can imagine what you want to do 199 00:11:37,317 --> 00:11:40,650 as a replacement for what those errors are, okay? 200 00:11:40,650 --> 00:11:41,330 See you next lecture.