1
00:00:00,960 --> 00:00:04,800
In the last lesson we made it possible to reuse the result of the first

2
00:00:04,800 --> 00:00:05,633
calculation.

3
00:00:06,060 --> 00:00:10,470
The answer for the first calculation became our input for the next calculation,

4
00:00:10,830 --> 00:00:14,370
allowing us to chain the calculations together. However,

5
00:00:14,400 --> 00:00:16,920
this created quite a bit of repetition in our code

6
00:00:17,100 --> 00:00:20,520
and it limits our user to just two calculations.

7
00:00:21,690 --> 00:00:25,950
Let's allow the user to chain as many calculations as they want to.

8
00:00:26,760 --> 00:00:29,100
And I want to throw this over to you as a challenge,

9
00:00:29,100 --> 00:00:33,000
so can you use a while loop and the input function to achieve this?

10
00:00:33,360 --> 00:00:34,040
Ask the user

11
00:00:34,040 --> 00:00:39,040
this question, type 'y' to continue calculating with the previous result or type 'n'

12
00:00:39,630 --> 00:00:40,463
to exit.

13
00:00:41,700 --> 00:00:45,570
If the user types y then your calculator should allow them to continue chaining

14
00:00:45,570 --> 00:00:48,120
together calculations with the previous answer.

15
00:00:48,780 --> 00:00:52,020
If they type anything else the program should exit for now.

16
00:00:52,440 --> 00:00:55,500
I'll give you a few seconds to pause the video before I show you the result.

17
00:00:57,960 --> 00:01:02,520
All right. So the easiest way to solve this is through the use of a while loop.

18
00:01:02,940 --> 00:01:07,620
So let's say that after we've asked for the first number and we've shown them

19
00:01:07,620 --> 00:01:10,440
all of the different symbols that they could possibly pick from,

20
00:01:10,860 --> 00:01:14,130
lets set a flag called should_continue.

21
00:01:15,990 --> 00:01:18,510
And we'll set that to start out as true.

22
00:01:19,500 --> 00:01:24,120
Then we can create a while loop and while it should continue as true,

23
00:01:24,540 --> 00:01:26,490
then we're going to perform these things.

24
00:01:27,120 --> 00:01:31,500
Now we want to change some of this text so that it's a little bit more reusable.

25
00:01:31,950 --> 00:01:34,410
Let's say pick an operation,

26
00:01:35,460 --> 00:01:40,460
and what's the next number instead of the second number.

27
00:01:41,220 --> 00:01:45,750
So now we have a first number, we have a operation,

28
00:01:45,780 --> 00:01:50,610
we have a second number and then we pick out the function from that dictionary

29
00:01:50,610 --> 00:01:53,400
of operations and we get our answer.

30
00:01:53,670 --> 00:01:58,670
So I'm going to change this back to answer and we're passing in num1

31
00:01:59,550 --> 00:02:03,960
and num2 into the calculation function. Now,

32
00:02:03,960 --> 00:02:06,120
once this line has been printed,

33
00:02:06,840 --> 00:02:10,199
everything that occurs afterwards is pretty much repetition.

34
00:02:11,160 --> 00:02:16,160
And instead we're going to ask the user to type y-- type y to continue calculating

35
00:02:19,380 --> 00:02:21,840
with the answer from the previous step.

36
00:02:22,440 --> 00:02:27,440
Let's make that an fstring and let's check what this is actually equal to.

37
00:02:28,380 --> 00:02:33,180
So remember that the input function is also a function that has an output,

38
00:02:33,720 --> 00:02:35,040
which is a little bit then zen,

39
00:02:35,250 --> 00:02:39,240
but the output of this function is whatever the user typed in.

40
00:02:39,360 --> 00:02:44,360
So if they typed in y then this part is going to become y. That's where we're

41
00:02:44,880 --> 00:02:45,750
going to check.

42
00:02:45,840 --> 00:02:50,840
So we're going to check if the result of the input from the user is equal to y,

43
00:02:51,360 --> 00:02:55,650
then in that case that means we should continue and it should repeat back.

44
00:02:56,100 --> 00:02:57,930
But when it loops back,

45
00:02:57,990 --> 00:03:02,990
we want to make sure that num1 is equal to the answer from the previous step.

46
00:03:04,930 --> 00:03:07,540
So we can say if this is true,

47
00:03:07,690 --> 00:03:11,740
then num1 is going to be set to equal the answer.

48
00:03:12,460 --> 00:03:15,640
But on the other hand, else, if they typed anything else,

49
00:03:15,940 --> 00:03:20,940
so we could say maybe type y to continue calculating with answer or type n to

50
00:03:22,780 --> 00:03:23,613
exit.

51
00:03:24,760 --> 00:03:29,760
So now if they type y then the answer is going to be set as the num1 back at

52
00:03:30,940 --> 00:03:32,800
the start of this while loop

53
00:03:33,100 --> 00:03:36,550
so that this num1 becomes the answer from the previous step.

54
00:03:37,090 --> 00:03:40,510
But if they typed n then we want this while loop to the end.

55
00:03:40,780 --> 00:03:44,110
So we're going to change the flag should_continue to false.

56
00:03:44,830 --> 00:03:46,840
So now let's give that a run.

57
00:03:50,670 --> 00:03:51,000
Right?

58
00:03:51,000 --> 00:03:52,890
Let's say the first number is five.

59
00:03:53,310 --> 00:03:57,570
Then we're going to add three to five and then we're going to type y to

60
00:03:57,570 --> 00:04:00,990
continue. So now we get to pick another operation,

61
00:04:01,020 --> 00:04:03,180
so let's multiply, um,

62
00:04:03,240 --> 00:04:08,240
eight by two and we get the result from the next step and we can actually keep

63
00:04:09,600 --> 00:04:12,360
going until we're basically done, right?

64
00:04:12,360 --> 00:04:16,980
So we can take the 16 and we can divide it by four.

65
00:04:17,610 --> 00:04:19,529
But if we type 'n' on the other hand,

66
00:04:19,800 --> 00:04:23,250
then our program ends and we see the prompt once more.

67
00:04:24,990 --> 00:04:26,790
And that's the solution to the challenge,

68
00:04:26,850 --> 00:04:30,540
but it's a little bit sad to just exit, right?

69
00:04:30,540 --> 00:04:33,270
Because with a calculator more often than not,

70
00:04:33,270 --> 00:04:37,950
what you want to do is to start a fresh calculation where you get to determine

71
00:04:37,980 --> 00:04:41,310
the first number and the second number once more.

72
00:04:42,540 --> 00:04:45,120
If the user didn't want to exit

73
00:04:45,570 --> 00:04:48,690
but if they wanted to start a new calculation,

74
00:04:49,230 --> 00:04:54,230
then how can we get them to go back all the way up here so that they provide the

75
00:04:54,720 --> 00:04:59,610
num1 as a fresh input? This is a little bit tricky

76
00:04:59,760 --> 00:05:03,750
and in programming, this concept is known as recursion.

77
00:05:04,260 --> 00:05:08,700
It's basically the idea that you could have a function that calls itself.

78
00:05:09,300 --> 00:05:14,300
So let's define a new function called calculator and this function takes no

79
00:05:14,820 --> 00:05:17,100
inputs and has no outputs.

80
00:05:17,640 --> 00:05:22,410
But all of this code that we've got here so far is inside the calculator

81
00:05:22,410 --> 00:05:25,230
function. To begin when we start,

82
00:05:25,350 --> 00:05:30,350
we have to call the calculator function in order for it to find the place where

83
00:05:30,450 --> 00:05:35,190
this function was defined and to actually carry out all of these instructions.

84
00:05:35,790 --> 00:05:36,180
Now,

85
00:05:36,180 --> 00:05:40,560
the next thing we get to do is a little bit interesting because when the user

86
00:05:40,560 --> 00:05:41,850
types no,

87
00:05:42,690 --> 00:05:47,280
and says that they don't want to continue calculating with the previous answer

88
00:05:47,640 --> 00:05:50,760
but instead they want to start a new calculation,

89
00:05:51,120 --> 00:05:54,270
what we want to do instead of just exiting the while loop,

90
00:05:54,630 --> 00:05:59,630
we want to call the calculator function because what this is going to do is it's

91
00:06:00,560 --> 00:06:05,030
basically going to take us all the way back up to the beginning where we get to

92
00:06:05,030 --> 00:06:07,430
enter a new input again.

93
00:06:08,510 --> 00:06:11,990
And remember that once you reach the end of a function,

94
00:06:12,350 --> 00:06:17,300
everything gets reset to the beginning. So should_continue becomes true again

95
00:06:17,570 --> 00:06:19,820
and this while loop will continue working.

96
00:06:20,600 --> 00:06:22,970
So let's try running this new version.

97
00:06:23,720 --> 00:06:28,720
We've performed a calculation and now we want to start a new calculation,

98
00:06:29,030 --> 00:06:33,710
so I'm going to type n. And now I get to again define the first number again.

99
00:06:37,100 --> 00:06:42,100
This recursion basically happens because we're calling this calculator function

100
00:06:42,890 --> 00:06:45,110
within the calculator function.

101
00:06:45,710 --> 00:06:49,490
Essentially the code runs and runs and runs until it reaches here

102
00:06:50,060 --> 00:06:55,010
and if these conditions are met and this calculator function is called,

103
00:06:55,250 --> 00:07:00,230
then it goes and finds the calculator function in order to call it once more.

104
00:07:01,310 --> 00:07:05,960
But now you have to be quite careful with while loops and with these recursive

105
00:07:05,960 --> 00:07:06,793
functions

106
00:07:06,920 --> 00:07:11,920
because let's say that instead of having all of this code and some checking code

107
00:07:12,500 --> 00:07:14,750
to determine when I should call calculator,

108
00:07:15,230 --> 00:07:18,830
if I actually just called calculator within calculator,

109
00:07:19,130 --> 00:07:21,590
then this is going to be a infinite loop.

110
00:07:21,620 --> 00:07:23,900
It's just going to keep going back and forth.

111
00:07:24,290 --> 00:07:27,410
The calculator function calls the calculator function,

112
00:07:27,440 --> 00:07:31,400
which goes back up and calls and it goes around and round around until forever.

113
00:07:32,090 --> 00:07:32,480
Again,

114
00:07:32,480 --> 00:07:36,620
be careful and make sure that there is some sort of condition that needs to be

115
00:07:36,620 --> 00:07:41,620
met in order for this function to call itself. In the next lesson,

116
00:07:41,810 --> 00:07:45,830
we're gonna add the finishing touches to our program and also fix a bug that you

117
00:07:45,830 --> 00:07:47,450
may have already spotted at this point.

