1
00:00:01,390 --> 00:00:04,180
Hey, welcome to today's bonus example.

2
00:00:06,660 --> 00:00:11,850
In the code experiment video you understood the difference between syntax errors and exceptions.

3
00:00:13,780 --> 00:00:21,670
Now there is another type of issue which neither the grammar checker or the interpreter.

4
00:00:22,560 --> 00:00:25,980
Nor the interpreter itself can capture.

5
00:00:26,010 --> 00:00:34,770
These are more logical issues which only humans can spot in a program, and for that you need to use

6
00:00:34,770 --> 00:00:35,990
different tools.

7
00:00:36,000 --> 00:00:39,420
So to discover what these issues are.

8
00:00:39,420 --> 00:00:40,240
Exactly.

9
00:00:40,260 --> 00:00:50,040
I will create a program which asks the user to enter the dimensions of a rectangle, and the program

10
00:00:50,040 --> 00:00:54,510
should output the area of the rectangle.

11
00:00:56,180 --> 00:00:58,280
So that would be something like.

12
00:00:59,630 --> 00:01:03,710
Width is equal to input.

13
00:01:09,160 --> 00:01:11,650
You ask the user to enter the rectangle width.

14
00:01:12,610 --> 00:01:14,020
Same for length.

15
00:01:23,570 --> 00:01:31,730
And so you want to convert this into floats so that you capture a number instead of strings, so convert

16
00:01:31,730 --> 00:01:36,380
the entire input function.

17
00:01:37,580 --> 00:01:42,470
Put it inside, float inside the parentheses over the float function.

18
00:01:45,460 --> 00:01:52,180
Event area would be equal to width times length.

19
00:01:53,550 --> 00:01:55,020
And you print out the area.

20
00:01:56,430 --> 00:01:57,720
So far, so good.

21
00:01:57,750 --> 00:02:03,810
So you run the program, you enter for for width five for length, and you get 20.

22
00:02:04,690 --> 00:02:14,220
But if the user enters four four with using a word instead of a number, then we would get this value

23
00:02:14,220 --> 00:02:14,730
error.

24
00:02:15,420 --> 00:02:19,500
So we want to tell the user, Oh, you need to enter an actual number.

25
00:02:19,500 --> 00:02:23,580
So for that of course we use try and accept as you learned already.

26
00:02:24,740 --> 00:02:30,010
So what we do is we indent all these lines and we put them inside.

27
00:02:30,020 --> 00:02:30,980
I try.

28
00:02:32,810 --> 00:02:34,610
Statements and.

29
00:02:36,690 --> 00:02:43,980
So we expect an error to happen in any of these, and that is a value error.

30
00:02:45,950 --> 00:02:56,420
If that happens, then we print out Please enter a number and note that we don't need the continuous

31
00:02:56,420 --> 00:02:59,510
statements here because we are not inside a wire loop.

32
00:02:59,720 --> 00:03:01,370
So that should be enough.

33
00:03:01,460 --> 00:03:05,480
Let's try again for and we have this.

34
00:03:05,480 --> 00:03:07,250
Please enter a number.

35
00:03:08,660 --> 00:03:10,070
So far so good.

36
00:03:10,070 --> 00:03:15,770
So we were able to successfully use try, accept.

37
00:03:17,230 --> 00:03:22,300
To catch a potential error that the user might trigger.

38
00:03:24,690 --> 00:03:30,780
But what if we don't want the user to enter?

39
00:03:31,790 --> 00:03:38,810
For example, if we don't want the user to enter five four with and the same number for length.

40
00:03:39,050 --> 00:03:44,090
So the program will say 25, but what if we want the program?

41
00:03:45,220 --> 00:03:50,860
To display a message to tell the user, Oh, wait, that is a square.

42
00:03:50,860 --> 00:03:57,720
And we we don't accept square calculations of various in our program.

43
00:03:57,730 --> 00:04:01,450
So we want to to show a message to the user.

44
00:04:01,990 --> 00:04:10,720
Of course, the try accept block would not accept that would not be able to to handle such a delicate

45
00:04:10,840 --> 00:04:18,070
requirements because that that is not an error logically for the interpreter that is fine the interpreter

46
00:04:18,070 --> 00:04:22,600
is simply multiplying two numbers, but this is an issue.

47
00:04:22,600 --> 00:04:25,030
We should handle it in another way.

48
00:04:25,750 --> 00:04:33,220
Now that is where if elif else conditional blocks come in handy again.

49
00:04:35,170 --> 00:04:43,990
In this code, we would write the if conditional after we get the width and the length so we could check

50
00:04:44,080 --> 00:04:47,920
if width is equal to length.

51
00:04:49,650 --> 00:04:51,660
And if that's the case.

52
00:04:53,810 --> 00:04:56,840
We exit the program with a message, we say.

53
00:04:58,760 --> 00:05:01,940
That's looks like a square.

54
00:05:03,900 --> 00:05:12,870
You could also live this empty and include a print function here and say here that that looks like a

55
00:05:12,870 --> 00:05:13,290
square.

56
00:05:13,290 --> 00:05:14,520
But this is.

57
00:05:16,020 --> 00:05:16,740
Less code.

58
00:05:16,740 --> 00:05:20,340
So this exit function now will.

59
00:05:22,000 --> 00:05:26,200
Break the program will interrupt it and we will print out this.

60
00:05:27,340 --> 00:05:29,080
String in the command line.

61
00:05:29,080 --> 00:05:30,370
So let's try it.

62
00:05:33,240 --> 00:05:33,930
Five.

63
00:05:34,530 --> 00:05:35,340
Five.

64
00:05:36,000 --> 00:05:37,290
And we get this.

65
00:05:37,290 --> 00:05:38,730
That looks like a square.

66
00:05:39,630 --> 00:05:48,420
The command line here in pie chart is also able to highlight this in red because it's being printed

67
00:05:48,420 --> 00:05:49,950
out by that exit function.

68
00:05:49,950 --> 00:05:55,530
So if you don't want that color, then you should use prints here.

69
00:05:55,530 --> 00:05:59,160
And that put that as I told you inside print.

70
00:06:02,080 --> 00:06:03,490
So what's going on here?

71
00:06:03,520 --> 00:06:06,370
Well, this is what happens.

72
00:06:06,370 --> 00:06:09,100
So we ask the user to answer some inputs.

73
00:06:09,670 --> 00:06:11,380
If there is an error.

74
00:06:12,530 --> 00:06:14,870
Then this will be executed.

75
00:06:14,870 --> 00:06:20,020
So if there is a value error, actually that's then this will be executed.

76
00:06:20,030 --> 00:06:24,260
If there is no error in here and in here as well.

77
00:06:24,320 --> 00:06:29,390
Then here the if conditional will check those two values.

78
00:06:29,600 --> 00:06:37,580
So you see that an if conditional can be used and it can be used without Elif and else.

79
00:06:38,390 --> 00:06:38,780
Here.

80
00:06:38,780 --> 00:06:42,290
It was not necessary to have leave or else.

81
00:06:42,440 --> 00:06:50,870
So and if conditional can be used when you want to check or check values, when you want to check conditions

82
00:06:50,870 --> 00:06:53,960
which do not produce errors.

83
00:06:53,960 --> 00:06:58,370
So they are programmatically correct.

84
00:06:58,370 --> 00:06:59,390
But.

85
00:07:00,620 --> 00:07:04,640
The product is not acceptable from us.

86
00:07:04,640 --> 00:07:13,280
So the requirements, for example, here, we don't want users for some reason to enter square dimensions.

87
00:07:13,290 --> 00:07:16,550
So we tell them, Oh, we don't accept that.

88
00:07:16,550 --> 00:07:17,930
That looks like a square.

89
00:07:19,620 --> 00:07:23,520
So if conditionals do not catch errors.

90
00:07:24,280 --> 00:07:25,750
They check values.

91
00:07:26,560 --> 00:07:29,020
That's something I wanted you to keep in mind.

92
00:07:29,020 --> 00:07:33,610
And that's what I was trying to demonstrate with this example.

93
00:07:34,270 --> 00:07:38,470
So the difference between tri accept and if conditionals.

94
00:07:39,990 --> 00:07:41,880
Thanks for following this and I'll talk to you later.

