1 00:00:00,389 --> 00:00:01,000 Well, 2 00:00:01,240 --> 00:00:05,130 back in the previous listing, we successfully created this particular program, 3 00:00:05,539 --> 00:00:09,609 they would accept inputs positive numbers from the user. 4 00:00:09,850 --> 00:00:14,090 It will study numbers in a list and it will continue to do so 5 00:00:14,300 --> 00:00:18,940 until the user types in the word done. And at the end of it, it will print out. 6 00:00:19,149 --> 00:00:20,540 If there was an even number, 7 00:00:20,549 --> 00:00:22,809 it will print out what the first even number was and 8 00:00:22,819 --> 00:00:25,829 will also eventually print out all the numbers in the list. 9 00:00:25,840 --> 00:00:26,559 However, 10 00:00:27,069 --> 00:00:31,850 one thing we discovered was that when we run the program and we added, let's say, 11 00:00:31,860 --> 00:00:35,450 for example, instead of an actual integer like eight or nine, 12 00:00:35,700 --> 00:00:38,369 we added, let's say 8.6 13 00:00:38,700 --> 00:00:39,270 right? 14 00:00:39,810 --> 00:00:40,810 When we press enter, 15 00:00:41,069 --> 00:00:44,220 we ended up having this particular row me 16 00:00:44,540 --> 00:00:45,529 a value arrow. 17 00:00:45,869 --> 00:00:47,610 Now if we run the program again, 18 00:00:48,229 --> 00:00:54,090 and instead of 8.5 I said, you know, iof I typed in letters instead I press enter, 19 00:00:54,380 --> 00:00:57,709 you can see once again that we also have the exact same error. 20 00:00:58,189 --> 00:01:01,889 And that's because our program has been created in such a way 21 00:01:02,270 --> 00:01:06,629 that it is expected to receive positive numbers only 22 00:01:06,779 --> 00:01:09,290 if we run the program one more time. 23 00:01:09,449 --> 00:01:13,449 And I typed in, let's say minus six, which is a negative number and I pressed enter, 24 00:01:13,940 --> 00:01:14,959 you can see 25 00:01:15,239 --> 00:01:16,940 that it actually 26 00:01:17,069 --> 00:01:19,730 kind of works in a way 27 00:01:19,930 --> 00:01:21,239 it does accept 28 00:01:21,629 --> 00:01:27,160 the negative six because it is an actual number. It just happens to be negative, 29 00:01:27,269 --> 00:01:27,839 but 30 00:01:28,650 --> 00:01:30,849 that still defeats the purpose because we only 31 00:01:30,860 --> 00:01:34,540 want the user to type in positive numbers. 32 00:01:34,889 --> 00:01:38,430 So it shouldn't even have accepted this negative six. 33 00:01:39,080 --> 00:01:43,099 So we do have quite some issues with our particular program. 34 00:01:43,110 --> 00:01:46,620 And we need to now introduce something known as the tri 35 00:01:47,470 --> 00:01:49,300 and then the except block. 36 00:01:49,559 --> 00:01:52,000 So the try block in Python is basically part 37 00:01:52,010 --> 00:01:55,209 of what we call an Exception handling mechanism, 38 00:01:55,900 --> 00:02:00,040 which would basically allow us to catch and handle errors that might occur 39 00:02:00,150 --> 00:02:02,870 during the execution of a code. 40 00:02:02,879 --> 00:02:07,239 So if an error occurs inside of the try block, the program will not crash. 41 00:02:07,279 --> 00:02:07,800 Instead, 42 00:02:07,809 --> 00:02:10,839 the error will be caught and the code inside 43 00:02:10,850 --> 00:02:15,279 the associated except block will then be executed. 44 00:02:15,520 --> 00:02:18,690 Keep in mind that it's not just about users 45 00:02:18,699 --> 00:02:21,199 making a mistake whenever they type in their inputs. 46 00:02:21,710 --> 00:02:27,009 It's also possible that a hacker will deliberately try to insert values that 47 00:02:27,020 --> 00:02:30,940 the program will not accept in an effort to actually crash the program. 48 00:02:31,220 --> 00:02:36,279 This is why this is why we need to always make sure that our programs, 49 00:02:36,440 --> 00:02:39,169 whenever they're accepting inputs from the user, 50 00:02:39,250 --> 00:02:43,610 we create and design them in such a way that they are able to handle 51 00:02:43,960 --> 00:02:47,330 our own inputs from the user. 52 00:02:47,449 --> 00:02:48,110 So 53 00:02:48,270 --> 00:02:50,279 how are we going to now 54 00:02:51,059 --> 00:02:52,970 catch any particular errors? 55 00:02:53,130 --> 00:02:55,309 Well, I'm gonna come over here 56 00:02:55,800 --> 00:02:57,610 where we're breaking. OK. 57 00:02:57,910 --> 00:02:58,520 So 58 00:02:58,970 --> 00:03:02,240 the reason why we're starting here is because this is where we're going to check 59 00:03:02,580 --> 00:03:03,839 whether or not 60 00:03:04,149 --> 00:03:06,279 the user has added 61 00:03:06,470 --> 00:03:10,880 either the done word or an actual number. 62 00:03:11,089 --> 00:03:15,059 So after break right after break, because break will check. OK. 63 00:03:15,070 --> 00:03:18,130 Is it done if it's not done that it's now going to execute? 64 00:03:18,139 --> 00:03:20,160 This is where we're now going to type in the words 65 00:03:20,320 --> 00:03:21,839 or rather the word try 66 00:03:22,080 --> 00:03:24,960 because this is where we want the program to catch 67 00:03:25,529 --> 00:03:27,440 an error if there is. 68 00:03:27,699 --> 00:03:30,119 So now it's going to be try and then colon 69 00:03:30,809 --> 00:03:32,470 and now this right here, 70 00:03:33,350 --> 00:03:34,369 we can simply 71 00:03:34,850 --> 00:03:35,770 tab that. 72 00:03:37,559 --> 00:03:38,259 OK? 73 00:03:38,389 --> 00:03:40,580 Let me just press enter right here. 74 00:03:40,860 --> 00:03:41,339 So 75 00:03:42,610 --> 00:03:43,639 we are saying 76 00:03:43,839 --> 00:03:44,610 right here 77 00:03:45,809 --> 00:03:46,639 that 78 00:03:47,009 --> 00:03:51,630 if the user types in done simply break the program, I'm sorry, break the loop. 79 00:03:51,869 --> 00:03:54,240 However, if it's an actual number, 80 00:03:54,559 --> 00:03:58,080 let us check to see if that number is positive. 81 00:03:58,279 --> 00:04:02,360 So right now we have created the variable num we added the 82 00:04:02,559 --> 00:04:05,639 user input to nu M. 83 00:04:05,740 --> 00:04:08,600 So now let us check to see if it is positive. 84 00:04:09,210 --> 00:04:11,250 The easiest way to check would be 85 00:04:11,419 --> 00:04:13,000 to simply say if 86 00:04:13,419 --> 00:04:14,800 the number 87 00:04:15,190 --> 00:04:17,769 is less than zero, 88 00:04:17,988 --> 00:04:19,809 this is the easiest way to check. 89 00:04:20,010 --> 00:04:23,350 So if the number is in fact less than zero, 90 00:04:23,709 --> 00:04:25,329 we're going to say print, 91 00:04:26,179 --> 00:04:29,519 please enter a positive number. 92 00:04:29,970 --> 00:04:32,369 OK. And now else 93 00:04:33,290 --> 00:04:34,809 because after eve comes, 94 00:04:35,000 --> 00:04:35,869 so now 95 00:04:37,579 --> 00:04:40,760 if it was in fact a positive number, 96 00:04:40,929 --> 00:04:43,160 we are now simply going to append, 97 00:04:43,410 --> 00:04:44,579 OK, we're going to append 98 00:04:44,929 --> 00:04:45,640 the 99 00:04:45,769 --> 00:04:48,309 num variable two numbers 100 00:04:48,579 --> 00:04:51,000 and that's it right here. 101 00:04:51,369 --> 00:04:53,790 OK. And finally, because we have try, 102 00:04:54,190 --> 00:04:55,720 we're going to close it out 103 00:04:56,380 --> 00:04:59,670 by using the accept statement. So basically 104 00:05:00,609 --> 00:05:01,929 what do, what he was saying? Hey, 105 00:05:02,809 --> 00:05:08,109 if the value provided by the user was in fact less than zero, 106 00:05:08,549 --> 00:05:09,510 then 107 00:05:09,679 --> 00:05:12,950 print, please enter a positive number. 108 00:05:13,190 --> 00:05:18,440 But then what if the user didn't type a number? What if they typed in letters 109 00:05:18,679 --> 00:05:20,350 or strings basically, right? 110 00:05:20,529 --> 00:05:24,459 So this is where the accept will now come in. So we're gonna say accept 111 00:05:24,640 --> 00:05:26,470 value error. 112 00:05:26,970 --> 00:05:31,390 OK? So if there's an actual error within the value itself, because it's not a number, 113 00:05:31,890 --> 00:05:33,279 we are now going to say 114 00:05:33,510 --> 00:05:37,399 uh print uh please enter 115 00:05:37,690 --> 00:05:39,829 a valid number 116 00:05:40,269 --> 00:05:42,730 and that's it right there. So 117 00:05:42,839 --> 00:05:45,420 let us try and see if this will work. 118 00:05:45,720 --> 00:05:47,320 So I'm gonna go ahead right now, 119 00:05:47,519 --> 00:05:48,839 run the program. 120 00:05:49,779 --> 00:05:50,480 OK? 121 00:05:50,640 --> 00:05:55,040 So what I'm gonna do right now is I'm going to add negative six press enter. 122 00:05:55,339 --> 00:05:59,160 You can see right here it says please enter a positive number. 123 00:05:59,519 --> 00:06:02,510 But what if I type in a string like this? For example, 124 00:06:02,679 --> 00:06:04,679 now it says please enter 125 00:06:05,059 --> 00:06:07,040 a valid number. 126 00:06:07,609 --> 00:06:09,670 You see how this works, right? 127 00:06:09,679 --> 00:06:12,399 So let me walk you through it all over again, 128 00:06:12,410 --> 00:06:16,589 basically right here where we're accepting the input from the user, 129 00:06:16,600 --> 00:06:18,170 we want to check to make sure 130 00:06:18,279 --> 00:06:20,820 that it's actually a positive number. 131 00:06:21,000 --> 00:06:25,510 The best way is if that number is less than zero, then we know it's negative, 132 00:06:25,519 --> 00:06:28,750 we can print out, please enter a positive number. 133 00:06:28,820 --> 00:06:32,290 If on the other hand, it is greater than zero, we know it's a positive number. 134 00:06:32,299 --> 00:06:35,989 We can go ahead right now and append the num to the numbers list. 135 00:06:36,769 --> 00:06:37,660 And now 136 00:06:37,980 --> 00:06:40,510 we will have to say, OK, but what if 137 00:06:40,779 --> 00:06:42,269 there was an actual 138 00:06:42,529 --> 00:06:47,070 error within the type of input that the user inserted? 139 00:06:47,079 --> 00:06:50,869 Maybe it wasn't a positive number or maybe it wasn't even a negative number. 140 00:06:51,000 --> 00:06:55,040 Maybe it was like a letters or words or something else entirely. 141 00:06:55,200 --> 00:06:58,950 We can then say please enter a valid number. 142 00:06:59,130 --> 00:07:02,519 That's where the accept comes into play. 143 00:07:02,899 --> 00:07:07,000 So that's it for the try and except statements. 144 00:07:07,260 --> 00:07:10,679 Thank you for watching the video. I will see you in the next class.