WEBVTT 1 00:00:00.730 --> 00:00:03.820 ok so let's start with a challenge so 2 00:00:03.820 --> 00:00:06.069 the challenges is for you to create a ok so let's start with a challenge so 3 00:00:06.069 --> 00:00:08.109 new python file and write a short the challenges is for you to create a 4 00:00:08.109 --> 00:00:10.449 program that asks the user to type 2 new python file and write a short 5 00:00:10.449 --> 00:00:12.760 integer numbers then prints out the program that asks the user to type 2 6 00:00:12.760 --> 00:00:16.090 first number / the second now the integer numbers then prints out the 7 00:00:16.090 --> 00:00:18.580 program shouldn't crash no matter what first number / the second now the 8 00:00:18.580 --> 00:00:20.619 the user types in although you shouldn't program shouldn't crash no matter what 9 00:00:20.619 --> 00:00:22.660 have to worry about control keys and the user types in although you shouldn't 10 00:00:22.660 --> 00:00:24.820 just as a hint here if you have to do have to worry about control keys and 11 00:00:24.820 --> 00:00:26.529 the same thing more than once that just as a hint here if you have to do 12 00:00:26.529 --> 00:00:29.560 sounds like a good use for a function so the same thing more than once that 13 00:00:29.560 --> 00:00:31.060 that's you challenge see how you go sounds like a good use for a function so 14 00:00:31.060 --> 00:00:33.010 pause the video and I'll see you when that's you challenge see how you go 15 00:00:33.010 --> 00:00:36.820 you get back ok so let's go through my pause the video and I'll see you when 16 00:00:36.820 --> 00:00:39.430 solution to this challenge and as you get back ok so let's go through my 17 00:00:39.430 --> 00:00:41.109 always yours might be different solution to this challenge and as 18 00:00:41.109 --> 00:00:42.969 the important thing here is that you've always yours might be different 19 00:00:42.969 --> 00:00:44.559 got a solution that works the important thing here is that you've 20 00:00:44.559 --> 00:00:46.420 not that you've got identical code to me got a solution that works 21 00:00:46.420 --> 00:00:49.420 so create a new file for this not that you've got identical code to me 22 00:00:49.420 --> 00:00:51.670 i'm going to right click over here click so create a new file for this 23 00:00:51.670 --> 00:00:54.730 on new select python file will call this i'm going to right click over here click 24 00:00:54.730 --> 00:00:59.890 one...okay we're on new select python file will call this 25 00:00:59.890 --> 00:01:01.120 gonna be asking for a number more than one...okay we're 26 00:01:01.120 --> 00:01:03.789 once so i put the code to get the number gonna be asking for a number more than 27 00:01:03.789 --> 00:01:06.340 from the user into a function so... once so i put the code to get the number 28 00:01:06.340 --> 00:01:10.930 ... from the user into a function so... 29 00:01:10.930 --> 00:01:45.070 ... ... 30 00:01:45.070 --> 00:01:49.990 so the function will keep asking for a ... 31 00:01:49.990 --> 00:01:52.479 number until something valid is entered so the function will keep asking for a 32 00:01:52.479 --> 00:01:54.880 to put everything into an infinite loop number until something valid is entered 33 00:01:54.880 --> 00:01:59.320 using while true on line 2 so that loop to put everything into an infinite loop 34 00:01:59.320 --> 00:02:01.390 just keeps looping so we'll have to use using while true on line 2 so that loop 35 00:02:01.390 --> 00:02:03.700 break or return statement to get out of just keeps looping so we'll have to use 36 00:02:03.700 --> 00:02:05.800 it obviously using the return statement break or return statement to get out of 37 00:02:05.800 --> 00:02:09.009 on line five next comes the try except it obviously using the return statement 38 00:02:09.009 --> 00:02:11.620 blocks so we've seen how to get numbers on line five next comes the try except 39 00:02:11.620 --> 00:02:14.320 from the keyboard a few times we now blocks so we've seen how to get numbers 40 00:02:14.320 --> 00:02:16.360 use the input function then convert the from the keyboard a few times we now 41 00:02:16.360 --> 00:02:19.540 users text into an int now that's use the input function then convert the 42 00:02:19.540 --> 00:02:21.310 the first place where an exception can users text into an int now that's 43 00:02:21.310 --> 00:02:23.560 be thrown when python attempts to the first place where an exception can 44 00:02:23.560 --> 00:02:26.260 convert the user's input into an int value be thrown when python attempts to 45 00:02:26.260 --> 00:02:28.510 now obviously the conversion convert the user's input into an int value 46 00:02:28.510 --> 00:02:31.060 to an int will only work if the users now obviously the conversion 47 00:02:31.060 --> 00:02:33.370 has actually entered numbers and to an int will only work if the users 48 00:02:33.370 --> 00:02:34.390 nothing else has actually entered numbers and 49 00:02:34.390 --> 00:02:36.459 so we need to wrap the conversion in a nothing else 50 00:02:36.459 --> 00:02:38.560 try block so we can trap that invalid so we need to wrap the conversion in a 51 00:02:38.560 --> 00:02:41.080 input so how did I know what exception try block so we can trap that invalid 52 00:02:41.080 --> 00:02:43.330 that we should be handling well one input so how did I know what exception 53 00:02:43.330 --> 00:02:44.620 approach to this would be to read that we should be handling well one 54 00:02:44.620 --> 00:02:46.510 through the list of exceptions in the approach to this would be to read 55 00:02:46.510 --> 00:02:48.190 documentation link that i showed you in through the list of exceptions in the 56 00:02:48.190 --> 00:02:50.230 the previous video that's certainly one documentation link that i showed you in 57 00:02:50.230 --> 00:02:51.370 way to find out the previous video that's certainly one 58 00:02:51.370 --> 00:02:53.380 now I'm a great believer in using way to find out 59 00:02:53.380 --> 00:02:55.450 documentation and I've been providing now I'm a great believer in using 60 00:02:55.450 --> 00:02:57.340 links to the documentation throughout documentation and I've been providing 61 00:02:57.340 --> 00:02:59.799 this course you really can't just write links to the documentation throughout 62 00:02:59.799 --> 00:03:01.870 code without reading documentation has this course you really can't just write 63 00:03:01.870 --> 00:03:04.660 lots of it but with that said I'm also code without reading documentation has 64 00:03:04.660 --> 00:03:06.519 the first to admit that program lots of it but with that said I'm also 65 00:03:06.519 --> 00:03:08.680 documentation isn't the most shall we the first to admit that program 66 00:03:08.680 --> 00:03:11.290 say riveting of reads so there's documentation isn't the most shall we 67 00:03:11.290 --> 00:03:12.670 another way to find out what we need to say riveting of reads so there's 68 00:03:12.670 --> 00:03:15.579 know i'll jump that as well so i'm going another way to find out what we need to 69 00:03:15.579 --> 00:03:17.140 to comment all that out for now and run know i'll jump that as well so i'm going 70 00:03:17.140 --> 00:03:18.459 this little program so let's just to comment all that out for now and run 71 00:03:18.459 --> 00:03:23.920 comment out the code the code we want to this little program so let's just 72 00:03:23.920 --> 00:03:28.299 run temporarily..... comment out the code the code we want to 73 00:03:28.299 --> 00:03:31.730 .... run temporarily..... 74 00:03:31.730 --> 00:03:37.700 alright so let's run this and we're .... 75 00:03:37.700 --> 00:03:39.650 going to enter a number obviously with a valid alright so let's run this and we're 76 00:03:39.650 --> 00:03:41.989 number we get no problems but we try going to enter a number obviously with a valid 77 00:03:41.989 --> 00:03:45.950 to run it with some garbage input you number we get no problems but we try 78 00:03:45.950 --> 00:03:47.750 see that the exception that we've raised to run it with some garbage input you 79 00:03:47.750 --> 00:03:49.760 here by putting in something invalid is see that the exception that we've raised 80 00:03:49.760 --> 00:03:51.980 a value error invalid literal for int with here by putting in something invalid is 81 00:03:51.980 --> 00:03:54.170 base 10 switching back to the a value error invalid literal for int with 82 00:03:54.170 --> 00:03:58.129 documentation if we look for our value base 10 switching back to the 83 00:03:58.129 --> 00:04:00.530 exception documentation if we look for our value 84 00:04:00.530 --> 00:04:04.190 sorry value error now obviously search for it exception 85 00:04:04.190 --> 00:04:05.870 but its a fair way down the list so sorry value error now obviously search for it 86 00:04:05.870 --> 00:04:08.060 chances are you might have to do a favor but its a fair way down the list so 87 00:04:08.060 --> 00:04:10.459 of reading to find it so either way you chances are you might have to do a favor 88 00:04:10.459 --> 00:04:12.200 could actually read this or you could of reading to find it so either way you 89 00:04:12.200 --> 00:04:14.569 have also gone back to the code and did could actually read this or you could 90 00:04:14.569 --> 00:04:16.070 what I did was try and get the program have also gone back to the code and did 91 00:04:16.070 --> 00:04:18.829 to crash without using try and except to what I did was try and get the program 92 00:04:18.829 --> 00:04:21.019 see what errors what exceptions are to crash without using try and except to 93 00:04:21.019 --> 00:04:22.430 actually generated in this case the see what errors what exceptions are 94 00:04:22.430 --> 00:04:25.070 value error either way we'll find out actually generated in this case the 95 00:04:25.070 --> 00:04:26.720 what we need but when dealing with value error either way we'll find out 96 00:04:26.720 --> 00:04:27.560 exceptions what we need but when dealing with 97 00:04:27.560 --> 00:04:29.960 I find personally it's often easier to exceptions 98 00:04:29.960 --> 00:04:31.729 just cause the problem and see what I find personally it's often easier to 99 00:04:31.729 --> 00:04:33.979 exceptions raised and then to add the try just cause the problem and see what 100 00:04:33.979 --> 00:04:35.960 except Clause after that exceptions raised and then to add the try 101 00:04:35.960 --> 00:04:38.419 alright let's delete that line that i except Clause after that 102 00:04:38.419 --> 00:04:40.820 added temporarily and go ahead and alright let's delete that line that i 103 00:04:40.820 --> 00:04:45.950 uncomment our function and just added temporarily and go ahead and 104 00:04:45.950 --> 00:04:47.450 carry on explaining more about what it uncomment our function and just 105 00:04:47.450 --> 00:04:48.110 does carry on explaining more about what it 106 00:04:48.110 --> 00:04:50.539 so if a valid number is entered on line does 107 00:04:50.539 --> 00:04:53.210 4 the code is going to continue and so if a valid number is entered on line 108 00:04:53.210 --> 00:04:55.610 the number will just be returned but if 4 the code is going to continue and 109 00:04:55.610 --> 00:04:58.039 invalid input has entered a value error the number will just be returned but if 110 00:04:58.039 --> 00:04:59.690 exception will be raised as we've just invalid input has entered a value error 111 00:04:59.690 --> 00:05:02.419 seen so we handle that error in our except exception will be raised as we've just 112 00:05:02.419 --> 00:05:04.729 Clause now we don't do a great deal is seen so we handle that error in our except 113 00:05:04.729 --> 00:05:06.200 not really much we can do apart from Clause now we don't do a great deal is 114 00:05:06.200 --> 00:05:07.910 asking the user to type another number not really much we can do apart from 115 00:05:07.910 --> 00:05:09.889 then we could perhaps make the message a asking the user to type another number 116 00:05:09.889 --> 00:05:11.990 bit more helpful or mention that only then we could perhaps make the message a 117 00:05:11.990 --> 00:05:13.340 digits are allowed bit more helpful or mention that only 118 00:05:13.340 --> 00:05:15.229 so you've got an extra point their gold digits are allowed 119 00:05:15.229 --> 00:05:16.760 star if you manage to do that in your so you've got an extra point their gold 120 00:05:16.760 --> 00:05:19.130 message now once that message in the star if you manage to do that in your 121 00:05:19.130 --> 00:05:20.840 exception blocks printed the code message now once that message in the 122 00:05:20.840 --> 00:05:22.849 continues so we'll go around the loop exception blocks printed the code 123 00:05:22.849 --> 00:05:25.099 the while loop again and that will keep continues so we'll go around the loop 124 00:05:25.099 --> 00:05:27.979 happening until a valid number is entered the while loop again and that will keep 125 00:05:27.979 --> 00:05:29.690 so we can be really confident this that happening until a valid number is entered 126 00:05:29.690 --> 00:05:32.389 this function will always return a valid so we can be really confident this that 127 00:05:32.389 --> 00:05:33.349 int value this function will always return a valid 128 00:05:33.349 --> 00:05:35.000 alright so how do we make use of this int value 129 00:05:35.000 --> 00:05:38.270 function well let's add get the two alright so how do we make use of this 130 00:05:38.270 --> 00:05:40.610 numbers from the users i'm going to put function well let's add get the two 131 00:05:40.610 --> 00:05:44.210 .. numbers from the users i'm going to put 132 00:05:44.210 --> 00:05:44.900 ... .. 133 00:05:44.900 --> 00:05:47.090 ... ... 134 00:05:47.090 --> 00:05:52.009 ...let's finish this ... 135 00:05:52.009 --> 00:05:54.919 off by performing the division now ...let's finish this 136 00:05:54.919 --> 00:05:56.360 we've seen in the previous video that off by performing the division now 137 00:05:56.360 --> 00:05:58.940 division by zero can raise a 0 division we've seen in the previous video that 138 00:05:58.940 --> 00:06:00.979 error exception i'm going to put the division by zero can raise a 0 division 139 00:06:00.979 --> 00:06:02.930 code to perform the division in a try error exception i'm going to put the 140 00:06:02.930 --> 00:06:05.720 block and catch the exception i'm gonna code to perform the division in a try 141 00:06:05.720 --> 00:06:08.810 come down here and then try... block and catch the exception i'm gonna 142 00:06:08.810 --> 00:06:10.400 ... come down here and then try... 143 00:06:10.400 --> 00:06:44.930 ... ... 144 00:06:44.930 --> 00:06:52.460 ok now using the same prompt twice isn't ... 145 00:06:52.460 --> 00:06:55.099 really very user-friendly a user might ok now using the same prompt twice isn't 146 00:06:55.099 --> 00:06:56.449 wonder what they've done wrong to get really very user-friendly a user might 147 00:06:56.449 --> 00:06:58.580 exactly the same prompt again so let's wonder what they've done wrong to get 148 00:06:58.580 --> 00:07:00.620 say actually easily fixed by passing the exactly the same prompt again so let's 149 00:07:00.620 --> 00:07:03.020 text we want to prompt the user want to say actually easily fixed by passing the 150 00:07:03.020 --> 00:07:05.150 prompt the user with as an argument to our text we want to prompt the user want to 151 00:07:05.150 --> 00:07:07.669 get int function so let's change this a prompt the user with as an argument to our 152 00:07:07.669 --> 00:07:09.349 little bit i'm going to come up here and get int function so let's change this a 153 00:07:09.349 --> 00:07:13.430 make this prompt and then what we're going to little bit i'm going to come up here and 154 00:07:13.430 --> 00:07:15.500 do number equals...instead of make this prompt and then what we're going to 155 00:07:15.500 --> 00:07:19.280 putting a hard-coded string there do number equals...instead of 156 00:07:19.280 --> 00:07:22.940 we're going to put prompt the prompt is putting a hard-coded string there 157 00:07:22.940 --> 00:07:23.960 going to be the message that we want we're going to put prompt the prompt is 158 00:07:23.960 --> 00:07:25.820 displayed when we're getting a number going to be the message that we want 159 00:07:25.820 --> 00:07:28.820 input and in terms of using the get int displayed when we're getting a number 160 00:07:28.820 --> 00:07:30.710 method now are the function now input and in terms of using the get int 161 00:07:30.710 --> 00:07:37.090 we're going to put... method now are the function now 162 00:07:37.090 --> 00:07:41.770 ... we're going to put... 163 00:07:41.770 --> 00:07:50.350 ... ... 164 00:07:50.350 --> 00:07:53.800 so let's try running this so ... 165 00:07:53.800 --> 00:07:55.360 please enter the first number we put some so let's try running this so 166 00:07:55.360 --> 00:07:56.949 rubbish numbers in there please enter the first number we put some 167 00:07:56.949 --> 00:07:59.199 invalid number entered please try again rubbish numbers in there 168 00:07:59.199 --> 00:08:01.750 and you can see I'm typing it again it's invalid number entered please try again 169 00:08:01.750 --> 00:08:03.160 going to continue asking for that first and you can see I'm typing it again it's 170 00:08:03.160 --> 00:08:05.260 number until we put something valid in going to continue asking for that first 171 00:08:05.260 --> 00:08:08.080 there so let's put a valid number like 455 number until we put something valid in 172 00:08:08.080 --> 00:08:10.810 in there now it's asking for the second there so let's put a valid number like 455 173 00:08:10.810 --> 00:08:12.700 number same deal there with invalid input in there now it's asking for the second 174 00:08:12.700 --> 00:08:14.530 is going to keep asking until I put number same deal there with invalid input 175 00:08:14.530 --> 00:08:15.550 something valid in there is going to keep asking until I put 176 00:08:15.550 --> 00:08:18.490 let's put 0 in as the value for the something valid in there 177 00:08:18.490 --> 00:08:21.970 second number and we get the error can't let's put 0 in as the value for the 178 00:08:21.970 --> 00:08:24.520 divide by 0 so this point I think we've second number and we get the error can't 179 00:08:24.520 --> 00:08:26.830 got a fairly robust program that the divide by 0 so this point I think we've 180 00:08:26.830 --> 00:08:29.289 user can't crash by entering invalid got a fairly robust program that the 181 00:08:29.289 --> 00:08:32.020 input so if your program behaves in the user can't crash by entering invalid 182 00:08:32.020 --> 00:08:33.849 same way congratulations input so if your program behaves in the 183 00:08:33.849 --> 00:08:35.349 now there are a number of ways that this same way congratulations 184 00:08:35.349 --> 00:08:37.390 could be written and you can even do now there are a number of ways that this 185 00:08:37.390 --> 00:08:39.159 away with the second try except block could be written and you can even do 186 00:08:39.159 --> 00:08:41.620 and use an if statement to check his away with the second try except block 187 00:08:41.620 --> 00:08:43.959 second number 0 instead that's also and use an if statement to check his 188 00:08:43.959 --> 00:08:46.390 fine and in this program is very little second number 0 instead that's also 189 00:08:46.390 --> 00:08:48.339 to choose between the two fine and in this program is very little 190 00:08:48.339 --> 00:08:50.860 methods now there's still one way the to choose between the two 191 00:08:50.860 --> 00:08:53.230 user can crash the program and that's to methods now there's still one way the 192 00:08:53.230 --> 00:08:55.570 do a control D which may be user can crash the program and that's to 193 00:08:55.570 --> 00:08:57.430 control z on earlier versions of do a control D which may be 194 00:08:57.430 --> 00:09:00.250 Windows now on operating systems derived control z on earlier versions of 195 00:09:00.250 --> 00:09:02.620 from unix that includes mac OS x and Windows now on operating systems derived 196 00:09:02.620 --> 00:09:05.560 linux ctrl-d triggers an end-of-file from unix that includes mac OS x and 197 00:09:05.560 --> 00:09:08.260 eof and Windows 10 behave in the same linux ctrl-d triggers an end-of-file 198 00:09:08.260 --> 00:09:10.810 way i'm going to show you what i mean by eof and Windows 10 behave in the same 199 00:09:10.810 --> 00:09:14.709 that by running it again and down here way i'm going to show you what i mean by 200 00:09:14.709 --> 00:09:19.209 if I do control d the first number now that by running it again and down here 201 00:09:19.209 --> 00:09:21.880 when I press ctrl-d on a Mac actually if I do control d the first number now 202 00:09:21.880 --> 00:09:24.250 get a different output when I press ctrl-d on a Mac actually 203 00:09:24.250 --> 00:09:25.990 that's because the controlled d is map get a different output 204 00:09:25.990 --> 00:09:27.459 to another process so what i'm going to do is that's because the controlled d is map 205 00:09:27.459 --> 00:09:32.260 cancel out of that and run this again so to another process so what i'm going to do is 206 00:09:32.260 --> 00:09:34.720 on a linux machine or Windows machine cancel out of that and run this again so 207 00:09:34.720 --> 00:09:37.089 you can try ctrl D from a mac what I have on a linux machine or Windows machine 208 00:09:37.089 --> 00:09:39.310 to do a command D simulate something you can try ctrl D from a mac what I have 209 00:09:39.310 --> 00:09:42.220 similar so i'm going to do that now when to do a command D simulate something 210 00:09:42.220 --> 00:09:44.200 i do that you can see I get this EOF similar so i'm going to do that now when 211 00:09:44.200 --> 00:09:46.030 error the end of file when reading a i do that you can see I get this EOF 212 00:09:46.030 --> 00:09:47.589 line so that's another exception that's error the end of file when reading a 213 00:09:47.589 --> 00:09:48.550 been raised line so that's another exception that's 214 00:09:48.550 --> 00:09:50.769 however if you're doing this on a window been raised 215 00:09:50.769 --> 00:09:52.899 machine or linux machine you might find however if you're doing this on a window 216 00:09:52.899 --> 00:09:54.369 that the program starts going a little machine or linux machine you might find 217 00:09:54.369 --> 00:09:57.850 bit nuts and that you have to start that the program starts going a little 218 00:09:57.850 --> 00:10:00.459 looking at manually stopping it bit nuts and that you have to start 219 00:10:00.459 --> 00:10:02.559 because at this stage the mac has looking at manually stopping it 220 00:10:02.559 --> 00:10:04.989 actually gracefully shut down the because at this stage the mac has 221 00:10:04.989 --> 00:10:06.759 program but you might find that it's actually gracefully shut down the 222 00:10:06.759 --> 00:10:08.199 looping continually windows and linux program but you might find that it's 223 00:10:08.199 --> 00:10:10.209 that's the case come over here and click looping continually windows and linux 224 00:10:10.209 --> 00:10:12.759 on stop process to stop it then cancel that's the case come over here and click 225 00:10:12.759 --> 00:10:14.799 the program if you happen to be running on stop process to stop it then cancel 226 00:10:14.799 --> 00:10:16.089 this from the command line like a the program if you happen to be running 227 00:10:16.089 --> 00:10:18.519 terminal session or from Windows 10 come this from the command line like a 228 00:10:18.519 --> 00:10:20.439 from a windows command prompt and terminal session or from Windows 10 come 229 00:10:20.439 --> 00:10:22.360 control C should probably stop as well from a windows command prompt and 230 00:10:22.360 --> 00:10:25.149 the bottom line here is that control control C should probably stop as well 231 00:10:25.149 --> 00:10:27.459 d signals to python that there's no the bottom line here is that control 232 00:10:27.459 --> 00:10:30.069 input left so consequently pythons d signals to python that there's no 233 00:10:30.069 --> 00:10:32.920 close the input stream so our code then input left so consequently pythons 234 00:10:32.920 --> 00:10:35.410 catches the exception and in the case of close the input stream so our code then 235 00:10:35.410 --> 00:10:38.379 windows at least and linux loops catches the exception and in the case of 236 00:10:38.379 --> 00:10:40.569 back around but the input streams windows at least and linux loops 237 00:10:40.569 --> 00:10:43.029 already been closed so the input back around but the input streams 238 00:10:43.029 --> 00:10:44.709 function returns immediately with already been closed so the input 239 00:10:44.709 --> 00:10:46.540 another end of file error our code function returns immediately with 240 00:10:46.540 --> 00:10:48.160 catches the end of file error and loops back around another end of file error our code 241 00:10:48.160 --> 00:10:51.489 and so on and so forth so we definitely catches the end of file error and loops back around 242 00:10:51.489 --> 00:10:53.170 don't want to do that even if the mac and so on and so forth so we definitely 243 00:10:53.170 --> 00:10:55.360 has only come up with one error don't want to do that even if the mac 244 00:10:55.360 --> 00:10:56.679 it's still not something not a has only come up with one error 245 00:10:56.679 --> 00:10:59.319 situation we want to get into so if your it's still not something not a 246 00:10:59.319 --> 00:11:01.179 user terminates input in this way situation we want to get into so if your 247 00:11:01.179 --> 00:11:03.670 respect their choice and allow the user terminates input in this way 248 00:11:03.670 --> 00:11:05.889 program to terminate there's nothing you respect their choice and allow the 249 00:11:05.889 --> 00:11:07.749 can really do about it anyway if you're program to terminate there's nothing you 250 00:11:07.749 --> 00:11:09.519 using python input function to read can really do about it anyway if you're 251 00:11:09.519 --> 00:11:12.610 from the user right so let's actually using python input function to read 252 00:11:12.610 --> 00:11:14.110 have a look at what we can do is close from the user right so let's actually 253 00:11:14.110 --> 00:11:18.369 down and what we can do in the case of have a look at what we can do is close 254 00:11:18.369 --> 00:11:19.959 an end of file error lets actually trap down and what we can do in the case of 255 00:11:19.959 --> 00:11:22.990 that and down here what we will do is up an end of file error lets actually trap 256 00:11:22.990 --> 00:11:25.269 here would be the place to do it after that and down here what we will do is up 257 00:11:25.269 --> 00:11:28.029 the accepted value error will get here would be the place to do it after 258 00:11:28.029 --> 00:11:31.209 another one except this time must be eof the accepted value error will get 259 00:11:31.209 --> 00:11:37.029 error we can do assist..... another one except this time must be eof 260 00:11:37.029 --> 00:11:40.480 we've got an error there and the error is error we can do assist..... 261 00:11:40.480 --> 00:11:41.949 that we need to basically it's we've got an error there and the error is 262 00:11:41.949 --> 00:11:43.689 unresolved reference so we need to that we need to basically it's 263 00:11:43.689 --> 00:11:47.319 import sys so lets go ahead and do that so unresolved reference so we need to 264 00:11:47.319 --> 00:11:48.910 ... import sys so lets go ahead and do that so 265 00:11:48.910 --> 00:11:55.780 ...and sys . exit ... 266 00:11:55.780 --> 00:11:57.940 terminates the program with the exit ...and sys . exit 267 00:11:57.940 --> 00:11:59.860 code that's used or past as its terminates the program with the exit 268 00:11:59.860 --> 00:12:01.930 argument now for normal program code that's used or past as its 269 00:12:01.930 --> 00:12:04.900 termination is quite usual to return 0 argument now for normal program 270 00:12:04.900 --> 00:12:07.720 and if you decide that the user boarding termination is quite usual to return 0 271 00:12:07.720 --> 00:12:08.980 the program isn't normal termination and if you decide that the user boarding 272 00:12:08.980 --> 00:12:10.930 then you can return a different value the program isn't normal termination 273 00:12:10.930 --> 00:12:14.140 i'll do that next let's just try running then you can return a different value 274 00:12:14.140 --> 00:12:15.820 this first to make sure that works i'll do that next let's just try running 275 00:12:15.820 --> 00:12:18.250 I've been here and going to do on this first to make sure that works 276 00:12:18.250 --> 00:12:20.590 a mac command d you would do a ctrl I've been here and going to do on 277 00:12:20.590 --> 00:12:23.950 D on windows or linux and we now just a mac command d you would do a ctrl 278 00:12:23.950 --> 00:12:25.870 got the program terminating without D on windows or linux and we now just 279 00:12:25.870 --> 00:12:29.380 anything else happening now if you are got the program terminating without 280 00:12:29.380 --> 00:12:31.540 writing a python script to be used anything else happening now if you are 281 00:12:31.540 --> 00:12:33.640 from the terminal possibly called from a shell writing a python script to be used 282 00:12:33.640 --> 00:12:36.130 script or Windows command file that it from the terminal possibly called from a shell 283 00:12:36.130 --> 00:12:38.200 may be really useful to use different script or Windows command file that it 284 00:12:38.200 --> 00:12:40.570 error codes or exit codes so we can may be really useful to use different 285 00:12:40.570 --> 00:12:42.580 return a exit code of one if the user error codes or exit codes so we can 286 00:12:42.580 --> 00:12:44.950 terminate the program and perhaps 2 if they attempt return a exit code of one if the user 287 00:12:44.950 --> 00:12:47.470 a division by 0 terminate the program and perhaps 2 if they attempt 288 00:12:47.470 --> 00:12:48.880 let's go ahead and add that code so a division by 0 289 00:12:48.880 --> 00:12:51.520 going to come up here to line 12 going let's go ahead and add that code so 290 00:12:51.520 --> 00:12:52.930 to change that to assists going to come up here to line 12 going 291 00:12:52.930 --> 00:12:56.140 sys.extone let's also put an to change that to assists 292 00:12:56.140 --> 00:12:58.390 error in here for the division sys.extone let's also put an 293 00:12:58.390 --> 00:13:01.090 by zero close the program now... error in here for the division 294 00:13:01.090 --> 00:13:05.230 ....you can see here process finished by zero close the program now... 295 00:13:05.230 --> 00:13:06.610 with exit code zero so let's try ....you can see here process finished 296 00:13:06.610 --> 00:13:10.480 running this again so now if we do my with exit code zero so let's try 297 00:13:10.480 --> 00:13:12.520 command d on a Mac or control d on windows running this again so now if we do my 298 00:13:12.520 --> 00:13:15.820 or linux we get process finished with command d on a Mac or control d on windows 299 00:13:15.820 --> 00:13:19.720 exit code one running it again this time or linux we get process finished with 300 00:13:19.720 --> 00:13:22.090 we'll put some valid numbers in 10 the exit code one running it again this time 301 00:13:22.090 --> 00:13:25.300 second number put zero to ensure the 0 division we'll put some valid numbers in 10 the 302 00:13:25.300 --> 00:13:29.020 error comes up can't divide by 0 this second number put zero to ensure the 0 division 303 00:13:29.020 --> 00:13:30.790 time we got process finished with exit code 2 error comes up can't divide by 0 this 304 00:13:30.790 --> 00:13:31.750 ... time we got process finished with exit code 2 305 00:13:31.750 --> 00:13:34.000 that's really good demonstration of ... 306 00:13:34.000 --> 00:13:36.400 reacting in a different way when that's really good demonstration of 307 00:13:36.400 --> 00:13:38.620 handling different types of operations reacting in a different way when 308 00:13:38.620 --> 00:13:41.290 alright let's finish the video here and handling different types of operations 309 00:13:41.290 --> 00:13:42.700 we're still got a couple more things we alright let's finish the video here and 310 00:13:42.700 --> 00:13:44.320 need to look at with exceptions we're still got a couple more things we 311 00:13:44.320 --> 00:13:51.030 let's start on that in the next video need to look at with exceptions