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