WEBVTT

0
00:00.080 --> 00:00.530
Now,

1
00:00.530 --> 00:08.030
there are so many possible solutions to create this Coffee Machine Program, satisfying all the requirements,

2
00:08.030 --> 00:13.220
and the way that you code it up is essentially your choice, right?

3
00:13.220 --> 00:19.850
Whether if you decide to use a while loop or use a for loop or create a different data structure, there

4
00:19.850 --> 00:21.650
are endless possibilities.

5
00:21.650 --> 00:25.580
What I'm going to show you now is just one of those possibilities.

6
00:25.580 --> 00:30.980
And what's really important is you don't feel like you've done it wrong just because it's different

7
00:30.980 --> 00:31.760
to mine.

8
00:31.760 --> 00:37.160
As long as it works the way that you expect it to, then consider yourself successful.

9
00:37.310 --> 00:43.460
The first thing I'm going to do is create a new project in PyCharm, and I'm going to call my project

10
00:43.460 --> 00:51.200
CoffeeMachine. Again, making sure that I've got the latest version of Python as the interpreter.

11
00:51.200 --> 00:52.700
I'll click create.

12
00:54.580 --> 01:00.460
Now the first thing I'm going to do here is right click on this and create a new file, which is going

13
01:00.460 --> 01:02.470
to be my main.py.

14
01:02.470 --> 01:06.430
And then I'm going to go to my starting project in Replit,

15
01:06.430 --> 01:12.940
and I'm just going to copy everything there is over here and paste it into this main.py.

16
01:13.030 --> 01:20.220
Now you might find the font of your code or of PyCharm a little bit too big or too small.

17
01:20.220 --> 01:28.200
If that's the case, you can just go into preferences and change the appearance and the font to a different

18
01:28.200 --> 01:28.650
size,

19
01:28.650 --> 01:30.840
and this is for the user interface.

20
01:30.840 --> 01:37.410
And if you want to change the font of the Editor, then you can go here, go to font and then change that

21
01:37.410 --> 01:42.520
size here I've tried to make it as large as possible so that when you're looking at this video on an

22
01:42.520 --> 01:46.210
iPad or an iPhone, all the code should still be readable.

23
01:46.210 --> 01:52.450
But of course, normally when you're coding, you'd probably want to fit more lines into the same screen,

24
01:52.450 --> 01:56.530
but it's not good to strain your eyes, so try to strike a balance there.

25
01:56.800 --> 02:02.140
Now I'm going to collapse that sidebar because I'm going to be coding entirely in this one file.

26
02:02.170 --> 02:06.190
Now notice that at the beginning we get given a MENU,

27
02:06.850 --> 02:11.380
and this is a dictionary which contains three entries,

28
02:11.380 --> 02:16.930
and each of those entries have a name of a drink: espresso, latte, and cappuccino.

29
02:16.930 --> 02:23.770
And then each of them have a value that holds a bunch of data, including the ingredients that are required

30
02:23.770 --> 02:26.680
to make that drink, and also the price of the drink.

31
02:26.800 --> 02:33.140
Now there's also a resources dictionary which holds the resources of the coffee machine.

32
02:33.500 --> 02:36.680
Now that we've got all of that, then we're ready to go.

33
02:36.860 --> 02:39.980
I'm going to tackle these requirements one by one.

34
02:39.980 --> 02:46.820
And the first one says to prompt the user by asking, "What would you like? (espresso, latte, or cappuccino)

35
02:46.820 --> 02:52.700
So I'm actually gonna just straight up copy this line and put that into an input.

36
02:53.120 --> 02:57.380
This is going to be saved into some sort of variable which I'll name choice.

37
02:58.250 --> 03:03.080
Now it tells me that the prompt should show every time action has completed,

38
03:03.080 --> 03:07.700
for example, once the drink is dispensed and it should show again, and again, and again.

39
03:07.700 --> 03:12.380
So that means we're probably going to have to embed this input in some sort of while loop.

40
03:12.380 --> 03:19.680
So I'm just going to say for now, while something is True, then keep asking for this prompt. Right

41
03:19.680 --> 03:26.760
now, if we decide to go and Run this code where we go to run and then click on this button and then

42
03:26.760 --> 03:32.400
select the main.py to run, then you can see that it asks me, "What would you like?"

43
03:32.400 --> 03:38.220
And if I put an input, it will keep asking me until eternity, basically, because there's currently

44
03:38.220 --> 03:41.040
no way of turning that True into False.

45
03:41.430 --> 03:43.740
So let's take a look at the next requirement.

46
03:43.740 --> 03:48.270
We should be able to turn off the coffee machine by entering "off" into the prompt.

47
03:48.330 --> 03:53.100
For maintainers of the coffee machine, they can use, off as the secret word to turn off the machine,

48
03:53.100 --> 03:56.190
and your code should end execution when this happens.

49
03:56.190 --> 04:00.090
So when somebody wants to buy coffee, this is the line that they see,

50
04:00.090 --> 04:05.280
but when a maintenance guy comes along, then they should be able to enter something,

51
04:05.280 --> 04:11.530
and if this choice happens to equal the secret code which is off, then in this case we should stop

52
04:11.530 --> 04:13.210
the while loop and exit.

53
04:13.210 --> 04:18.880
So that gives us a way of changing this true into some other form of variable.

54
04:18.880 --> 04:19.270
Right?

55
04:19.270 --> 04:22.420
So we could create a new variable called is_on.

56
04:22.420 --> 04:24.340
Start it off as True,

57
04:24.340 --> 04:31.630
and while the machine is_on, then it should continue to loop through and ask the user for their choice,

58
04:31.630 --> 04:37.750
but if the choice happens to be off, then we're going to turn that is_on into False.

59
04:38.440 --> 04:40.630
Now if we run our code again,

60
04:40.630 --> 04:45.070
so now that you've run it once, you can either stop it or you can rerun it.

61
04:45.070 --> 04:49.810
And now it's going to stop your existing code and rerun the code.

62
04:49.810 --> 04:54.710
So if you don't want to see this dialog every time, then just check this box, and then click Stop and

63
04:54.710 --> 04:55.550
Rerun.

64
04:55.670 --> 05:02.330
So now if we make a selection or we say something, basically anything other than the keyword which

65
05:02.330 --> 05:06.890
is off, it's going to loop back and forth and keep prompting us.

66
05:06.890 --> 05:13.610
But if I say off, then the machine turns off and you can see that I've now exited the program.

67
05:14.510 --> 05:16.880
Now we've tackled 1 and 2,

68
05:16.880 --> 05:18.560
let's go on to Number 3.

69
05:18.590 --> 05:24.050
When the user enters the keyword "report" to the prompt, another secret word,

70
05:24.080 --> 05:29.600
a report should be generated that shows the current resource values, for example, water, milk, coffee

71
05:29.600 --> 05:30.470
and money.

72
05:30.470 --> 05:32.210
So how can we do that?

73
05:32.210 --> 05:36.470
Well, firstly, we don't actually have a variable that holds the amount of money.

74
05:36.470 --> 05:37.910
So let's create something.

75
05:37.910 --> 05:42.150
Let's call it profit maybe and set it to equal 0 to begin with.

76
05:42.150 --> 05:47.580
Our machine has an empty money box in the beginning, so now we have to check to see.

77
05:47.580 --> 05:51.630
Well, elif, the choice was equal to report,

78
05:51.630 --> 05:54.630
well, in this case, we have to generate a report.

79
05:54.630 --> 06:00.690
And the report is basically going to print all the values of these resources.

80
06:00.690 --> 06:07.020
So I'm simply going to copy the expected output and I'm going to paste it here,

81
06:07.140 --> 06:15.240
and then we can try and turn that into print statements, making it dynamic instead of hardcoded.

82
06:15.330 --> 06:19.620
In my case, I want to add print in front of all of these lines.

83
06:19.620 --> 06:23.940
And previously we've been doing this just by writing it one by one,

84
06:23.940 --> 06:26.070
and then maybe we could copy and paste it.

85
06:26.070 --> 06:32.020
But let me show you a quick tip that you can do in PyCharm. If you're on Windows, hold down the Alt

86
06:32.020 --> 06:34.360
and the shift key on your keyboard.

87
06:34.360 --> 06:37.990
If you're on a Mac, hold down the option and the shift key.

88
06:37.990 --> 06:42.940
Now click at the beginning and hold and drag down.

89
06:43.270 --> 06:45.400
So if that doesn't work, try it a few times,

90
06:45.400 --> 06:46.930
you'll get the hang of it eventually.

91
06:46.930 --> 06:49.360
But notice how I've now got four cursors.

92
06:49.360 --> 06:51.640
And that means when I write print.

93
06:51.640 --> 06:52.780
Check this out.

94
06:53.500 --> 06:54.880
Isn't that cool?

95
06:55.360 --> 07:01.660
I've managed to write on four lines at once because I need that repeat functionality, and this is a

96
07:01.660 --> 07:04.120
way of doing multi-line editing.

97
07:04.120 --> 07:08.170
Remember that shortcut and use it in the future if you find it useful.

98
07:08.170 --> 07:14.920
So I'm actually going to change this all into f-strings because I want to change these numbers, instead

99
07:14.920 --> 07:19.900
of being hardcoded, I want to insert them into here using the curly braces.

100
07:19.930 --> 07:26.440
The water is stored under resources and then it's in the key called "water".

101
07:27.490 --> 07:31.660
And now notice how I've got a outer double quote,

102
07:31.660 --> 07:33.700
so I can't have an inner double quote.

103
07:33.700 --> 07:39.790
So I'm going to change this into single quotes instead, like so.

104
07:39.790 --> 07:42.800
And I'm going to do the same for milk and coffee.

105
07:44.180 --> 07:48.560
So now I've added the water, milk and coffee into my print statement.

106
07:48.560 --> 07:50.990
All I have left is the money.

107
07:50.990 --> 07:56.630
So let's delete the value and let's insert that profit into here.

108
07:56.750 --> 08:00.440
Now let's run our code again and let's check it out.

109
08:00.440 --> 08:08.180
If I type report, it should now give me a report of all the current values, and money is equal to $0

110
08:08.180 --> 08:10.280
because that's what we start off with.

111
08:10.910 --> 08:13.790
We're now ready to tackle number four.

112
08:13.820 --> 08:20.270
Here we have to check that when the user chooses a drink, we're going to check if there are enough

113
08:20.270 --> 08:23.480
resources to make that particular drink they chose.

114
08:23.510 --> 08:29.970
For example, if a latte requires 200ml of water, but there's only 100ml left in the machine, it

115
08:29.970 --> 08:33.060
should not make the drink as it actually can't make the drink,

116
08:33.060 --> 08:34.440
and it's going to print out,

117
08:34.440 --> 08:39.450
"Sorry, there's not enough water," or "not enough milk," or "not enough coffee," whatever it may be.

118
08:39.450 --> 08:42.450
So let's tackle this particular checkpoint.

119
08:43.620 --> 08:48.720
Now that I've got the if choice is equal off, if choice is equals report,

120
08:48.720 --> 08:54.120
now if it's not either of those, then they're probably going to be entering the name of a drink.

121
08:54.120 --> 08:57.120
So let's catch that using an else statement.

122
08:57.120 --> 09:03.030
And then inside this else statement, I'm going to get hold of the particular drink that they ordered

123
09:03.030 --> 09:05.790
by tapping into our MENU dictionary.

124
09:05.790 --> 09:09.240
And then using that choice they typed in as the key.

125
09:09.420 --> 09:14.340
Let's say that the particular drink that they chose is equal to MENU,

126
09:14.340 --> 09:18.640
and then the key is of course going to be the choice.

127
09:18.640 --> 09:24.070
So now if I just print this drink and I run my code...

128
09:24.070 --> 09:28.720
So the shortcut for running is actually holding down the control and R.

129
09:28.720 --> 09:34.180
And now I can go down here and you can see it's asking me for what I would like.

130
09:34.180 --> 09:35.920
So I'm going to choose latte.

131
09:36.160 --> 09:42.590
And what's going to be printed is the latte entry in my recipes dictionary up here.

132
09:42.590 --> 09:50.450
So this particular value, now that I've got this value stored inside a variable called drink, well

133
09:50.450 --> 09:57.440
then I can tap into its ingredients and loop through each of the ingredients, comparing it against

134
09:57.440 --> 10:00.740
the resources and seeing if there's enough.

135
10:00.740 --> 10:05.360
Now this is a little bit of functionality that should probably be self-contained.

136
10:05.360 --> 10:10.550
So instead of just printing the drink I'm actually going to create a new function.

137
10:10.550 --> 10:18.170
So up here I'm going to create a new function with our def and I'm going to call it, is_resource_sufficient().

138
10:20.060 --> 10:27.920
And this is_resource_sufficient() is going to take the order_ingredients as an input,

139
10:27.920 --> 10:29.900
and then it's going to work on that.

140
10:29.900 --> 10:36.170
So if we want to call that function and pass in the order_ingredients, we'll have to call

141
10:36.170 --> 10:37.100
is_resource_efficient()

142
10:37.100 --> 10:45.170
and then the order_ingredients will be from the drink and then getting hold of the values under the

143
10:45.170 --> 10:46.610
key, ingredients.

144
10:49.280 --> 10:54.420
So under this particular key it will fetch this particular dictionary.

145
10:54.420 --> 10:59.760
And this is the dictionary that's going to be passed over to this function as the input.

146
10:59.790 --> 11:05.910
So now that we have a hold of a dictionary with all the ingredients that are required and the amount

147
11:05.910 --> 11:12.060
of each ingredient, we can now compare it against our resources, which is a very similar dictionary

148
11:12.060 --> 11:16.620
with the resources and the amount that's left in the machine.

149
11:17.490 --> 11:24.780
We can loop through the order_ingredients, and for each of the items in the ingredients, we're going

150
11:24.780 --> 11:29.970
to check to see if the order_ingredients at that particular key,

151
11:29.970 --> 11:39.300
so this is getting hold of the value, is greater than or equal to the resources using the same particular

152
11:39.300 --> 11:39.930
key.

153
11:39.990 --> 11:46.720
For example, if we were looking at the first example, the item would be equal to water,

154
11:46.870 --> 11:54.160
so if we fetch the value from order_ingredients with the key of water, we should get hold of 200,

155
11:54.160 --> 12:03.970
and we would now test to see if 200 is greater than or equal to the 300 that we have under the resources,

156
12:03.970 --> 12:09.340
well, in this case then we should probably tell the user that we actually can't make it.

157
12:09.340 --> 12:11.470
So let's put an if statement there.

158
12:11.920 --> 12:16.780
And I'm going to use this same string here to print it out.

159
12:18.130 --> 12:26.830
Now notice when I pasted that string in and it has the double quotes from the PDF file here that it's

160
12:26.830 --> 12:28.390
actually not being recognized,

161
12:28.390 --> 12:29.920
and I'm getting an error here.

162
12:29.930 --> 12:35.210
And the important thing to note is that there's a difference between decorative double quotes like these,

163
12:35.210 --> 12:38.810
which look different for the beginning quote and the end quote,

164
12:38.810 --> 12:43.070
and then there are programming double quotes which look like this.

165
12:43.070 --> 12:46.850
So I'm going to select this whole line and I'm going to add a double quote,

166
12:46.850 --> 12:51.560
and notice how they look identical from the front and the back.

167
12:52.490 --> 12:54.170
So now it's going to print,

168
12:54.170 --> 12:56.030
"Sorry, there is not enough..."

169
12:56.030 --> 12:58.580
And the enough of what?

170
12:58.580 --> 13:02.210
It's going to be the item that we're currently looping through.

171
13:02.210 --> 13:08.330
So let's change that to an f-string, which makes that an active piece of code that's going to be inserted.

172
13:08.630 --> 13:14.750
And in this case, we're going to return False because there is not enough resources.

173
13:14.750 --> 13:21.900
But otherwise, if we managed to get to the end of the for loop and we still haven't returned or exited

174
13:21.900 --> 13:26.730
the function by returning False, then, in this case, we can return True.

175
13:26.730 --> 13:35.040
So if this particular logic is a little bit confusing for you, you could have something like like this.

176
13:35.040 --> 13:37.500
So you have is_enough == True?

177
13:37.500 --> 13:46.980
And you could change is_enough to False if any of the order_ingredients are greater than the resources.

178
13:46.980 --> 13:51.270
And finally at the end you could return is_enough.

179
13:51.270 --> 13:55.890
So basically it stays true unless one of these if statements gets activated.

180
13:56.520 --> 14:01.680
But for simplicity's sake, I'm actually just going to keep it simple like this.

181
14:02.070 --> 14:04.860
And we're now ready to receive that result here.

182
14:04.860 --> 14:07.300
So we can put an if statement here.

183
14:07.300 --> 14:14.890
If the resources are sufficient for the drink, then we can proceed to continue to the next step.

184
14:15.700 --> 14:18.610
The next step is to process coins.

185
14:18.610 --> 14:23.530
The user is going to be asked for the number of quarters they have, the number of dimes, nickels and

186
14:23.530 --> 14:24.190
pennies.

187
14:24.190 --> 14:26.890
And you have to remember their values.

188
14:26.890 --> 14:29.590
So if you're from the US, this shouldn't be a problem,

189
14:29.590 --> 14:35.530
but if you're like me, somebody who's not from the US,  I actually find it really confusing

190
14:35.530 --> 14:37.180
when I go to the States,

191
14:37.180 --> 14:43.030
I always think that the larger coin that the nickel should be worth more than the dime, but I think

192
14:43.030 --> 14:44.320
that's just me being silly.

193
14:44.920 --> 14:50.140
So we're going to ask the user to insert some coins, we're going to process it, and then we're going

194
14:50.140 --> 14:53.440
to calculate the total value of the coins they inserted.

195
14:53.440 --> 14:57.700
That to me sounds like it should be a separate function as well.

196
14:57.700 --> 15:02.290
So let's create another function here which I'm going to call process_coins().

197
15:03.640 --> 15:10.840
And this is not going to take any inputs, but it is going to return the total value of the coins inserted.

198
15:10.960 --> 15:13.000
Now how do we process coins?

199
15:13.000 --> 15:22.040
Well first we can print to ask them to please insert coins, and then afterwards we're going to somehow

200
15:22.040 --> 15:23.480
calculate a total.

201
15:23.480 --> 15:23.720
Right?

202
15:23.720 --> 15:26.090
This is the variable that we're going to keep track of,

203
15:26.090 --> 15:29.420
and we're going to return as the output of this function.

204
15:29.720 --> 15:34.370
The total is going to be calculated based on the four types of coins.

205
15:34.370 --> 15:38.780
So the first question we're going to ask them is, "How many quarters?"

206
15:40.280 --> 15:43.640
And this of course is going to be a whole number,

207
15:43.640 --> 15:47.150
so we're going to turn it from a string into an integer.

208
15:47.150 --> 15:53.120
And we know that each quarter is worth 0.25 of a dollar.

209
15:53.120 --> 15:57.590
So we can multiply the number of quarters by 0.25,

210
15:57.590 --> 15:59.840
and then we'll get the monetary value.

211
16:00.860 --> 16:06.720
Now we'll need to do the same thing for a bunch of other coins.

212
16:06.720 --> 16:12.930
So instead of quarters, this is going to be dimes, and then it's going to be nickels,

213
16:13.200 --> 16:16.080
and finally, it's going to be pennies.

214
16:16.440 --> 16:25.290
Dimes are worth $0.10, nickels are worth $0.05 and pennies are worth $0.01.

215
16:25.440 --> 16:31.560
Now, instead of just setting the totals at each of these values, every subsequent one other than the

216
16:31.560 --> 16:38.040
first one which, remember, creates this variable and sets its value, every other one is just going

217
16:38.040 --> 16:40.800
to be added to the current value.

218
16:40.800 --> 16:41.850
Like this.

219
16:42.300 --> 16:48.420
So now at the very end of all of this, we're going to return the total as the output.

220
16:48.420 --> 16:54.940
And whenever you have something that returns like both of these functions, you should probably be adding

221
16:54.940 --> 16:55.870
a docstring.

222
16:55.870 --> 17:03.910
So in this case, this """Returns the total calculated from coins inserted."""

223
17:04.720 --> 17:17.800
And in this case, what happens is it returns True when order can be made, and False if ingredients

224
17:17.800 --> 17:19.510
are insufficient.

225
17:20.740 --> 17:25.330
Now let's call this function that we created process_coins().

226
17:25.570 --> 17:31.600
If there's enough resources to make the drink, then the next step is to actually ask them for the money,

227
17:31.600 --> 17:35.080
so here is where we're going to call process coins.

228
17:35.080 --> 17:41.470
And notice when I write this and I hover over it, you can see that docstring we just wrote, "Returns

229
17:41.470 --> 17:43.850
the total from coins inserted."

230
17:44.780 --> 17:52.070
That means this is going to return, and we need to capture the user's payment in this variable.

231
17:52.070 --> 17:56.840
So this returns the output, replaces this function call,

232
17:56.840 --> 18:00.560
and then it gets saved inside this variable called payment.

233
18:00.590 --> 18:03.290
Now what are we going to do with this payment.

234
18:03.290 --> 18:09.050
Well that goes on to the next step which is to check that the transaction was successful.

235
18:09.050 --> 18:14.420
So we have to make sure that the user has inserted enough money to actually purchase the drink they

236
18:14.420 --> 18:15.140
wanted.

237
18:15.140 --> 18:18.110
But each drink, of course has a different price,

238
18:18.110 --> 18:23.000
so if the user inserts enough money, then we're going to give them some change.

239
18:23.000 --> 18:28.040
But if they haven't inserted enough money, then we're going to say, "Sorry, that's not enough money..."

240
18:28.040 --> 18:29.690
And the money is refunded.

241
18:29.930 --> 18:35.340
But if they have inserted enough money, then the cost of the drink is going to be added to the machine

242
18:35.340 --> 18:36.420
as the profit.

243
18:36.420 --> 18:42.000
So the next time when we trigger the report, then we're going to get to see the increase in the monetary

244
18:42.000 --> 18:42.780
value.

245
18:43.860 --> 18:49.650
Again let's create a new function, and let's get rid of some of these squiggly lines by adding enough

246
18:49.650 --> 18:51.780
spaces in between the functions.

247
18:52.380 --> 18:56.580
This one I'm going to call is_transaction_successful().

248
18:58.020 --> 19:01.020
Because that's basically what we're going to be checking.

249
19:01.020 --> 19:03.930
And it's going to take two inputs,

250
19:03.930 --> 19:08.760
It's going to take an input in terms of the amount of money that was received,

251
19:08.760 --> 19:14.250
and it's also going to have another input, which is the cost of the drink.

252
19:14.880 --> 19:26.140
This function's goal is to """Return True when the payment is accepted, or it's going to return False

253
19:26.140 --> 19:30.070
if the money is insufficient."""

254
19:32.080 --> 19:36.580
Notice how there's this line to the right here of your editor.

255
19:36.580 --> 19:44.350
Basically, what happens is if you have a line of code that's a little bit too long past the recommendation

256
19:44.350 --> 19:50.890
from PEP 8, you can see that PEP 8 recommends that a line should not be longer than 120 characters,

257
19:50.890 --> 19:54.730
because it's very hard to read for somebody scrolling around like this.

258
19:54.730 --> 20:00.070
So in this case, they would want you to put it onto a new line so that you don't have to scroll and

259
20:00.070 --> 20:02.260
you can see it all on the same screen.

260
20:02.410 --> 20:08.480
But in our case, this is not a problem because we have not exceeded the line length recommendation.

261
20:09.290 --> 20:14.030
So how are we going to check if the transaction is successful?

262
20:14.060 --> 20:20.570
Well, if the money_received is greater or equal to the cost of the drink,

263
20:20.840 --> 20:23.090
well, in this case, that means we should return

264
20:23.090 --> 20:24.080
True, right?

265
20:24.110 --> 20:26.600
The transaction is successful.

266
20:26.600 --> 20:32.510
And if it's not the case, if it's the opposite case, then we're going to print.

267
20:32.510 --> 20:36.800
"Sorry, that's not enough money..." and the money is refunded to them.

268
20:39.290 --> 20:41.120
So let's print that here.

269
20:41.630 --> 20:44.630
And we're also going to return False.

270
20:44.660 --> 20:48.350
Remember that the return has to be the last thing in your function,

271
20:48.350 --> 20:52.970
if you put this above the print statement then the print statement will never get called.

272
20:52.970 --> 20:54.800
And that's why you have this highlight.

273
20:54.810 --> 20:59.010
And if you click on it, you can see that it tells you, "This code is unreachable."

274
21:00.240 --> 21:05.760
So there's a lot of these little hints and tips that really help you when you're developing.

275
21:05.910 --> 21:11.910
But if the user has inserted enough money, then the cost of the drink should be added to the machine

276
21:11.910 --> 21:16.140
as the profit so that we can see it in the next time report is triggered.

277
21:16.140 --> 21:23.580
So this means that if this money_received is greater or equal to the drink_cost, then we're going to

278
21:23.580 --> 21:30.360
add to this variable called profit that we have up here, which starts out at 0,

279
21:30.360 --> 21:34.470
but we're going to add the drinks_cost to profit.

280
21:34.470 --> 21:37.740
So we're going to say profit += drinks_cost.

281
21:37.740 --> 21:47.230
And now you'll see an error under the profit because this is acting inside a local scope and profit

282
21:47.230 --> 21:50.590
is outside in the global scope.

283
21:50.590 --> 21:54.730
So in order to reach it, we have to say global profit.

284
21:55.870 --> 22:03.400
And the final part of checking the transaction is seeing if the user has inserted too much money, then

285
22:03.400 --> 22:05.260
the machine is going to offer change.

286
22:05.260 --> 22:11.500
For example, here is however many dollars in change and the change should be rounded to two decimal

287
22:11.500 --> 22:12.520
places.

288
22:12.760 --> 22:16.000
Again, it's going to be inside this if statement.

289
22:16.120 --> 22:23.920
So the change is going to be equal to the amount of money_received subtracting the drink_cost.

290
22:23.920 --> 22:27.640
And this of course could be any number of decimal places.

291
22:27.790 --> 22:34.240
So we can use the round function which you've seen a long time ago to round this number,

292
22:34.240 --> 22:38.380
and the second input is the number of decimal places.

293
22:38.980 --> 22:43.450
So if you just hover over the function name then you can see the docs come up.

294
22:43.450 --> 22:50.050
And this function is basically going to round a number to a given precision in decimal digits.

295
22:50.050 --> 22:57.110
So the first is the number you want to round, and the second is the number of digits that you want after

296
22:57.110 --> 22:58.640
the dot, basically.

297
22:59.570 --> 23:06.980
Now that we've gotten hold of the change, we're going to print and tell the user, basically, "Here is

298
23:06.980 --> 23:09.860
this many dollars in change..."

299
23:10.850 --> 23:14.990
And of course I have to add an f to activate that f- string.

300
23:16.250 --> 23:21.200
So now we're ready to call is_transaction_successful,

301
23:21.200 --> 23:25.760
and we're going to pass in the money_received and the drink_cost.

302
23:25.760 --> 23:31.640
So the money_received is of course going to be the payment from the previous step that was calculated

303
23:31.640 --> 23:32.840
from all the coins,

304
23:32.840 --> 23:36.530
and the drink_cost is going to be based on the drink,

305
23:36.530 --> 23:41.120
and it's under the key, cost, which we can confirm up here.

306
23:41.120 --> 23:43.830
So the drink is this dictionary.

307
23:43.830 --> 23:46.770
And then there's the ingredients and the cost.

308
23:48.780 --> 23:53.610
Let's rerun our code and let's test it out with something.

309
23:53.610 --> 23:55.350
Let's say I want a latte.

310
23:55.590 --> 23:57.150
"Please insert coins."

311
23:57.150 --> 24:02.370
Let's say we tried to insert insufficient coins,

312
24:03.450 --> 24:05.850
it tells us, "Sorry, that's not enough money.

313
24:05.850 --> 24:07.380
Money is refunded."

314
24:07.380 --> 24:11.250
But let's try giving it enough coins this time.

315
24:13.350 --> 24:16.890
And in this case, it accepts it and it tells us,

316
24:16.890 --> 24:23.460
"Here's $2.42 in change," rounded to two decimal places, and we're ready to take another drink.

317
24:23.460 --> 24:30.240
So now if we hit report, you can see that we've now earned some money in our machine and all our code

318
24:30.240 --> 24:32.710
is working as expected.

319
24:33.220 --> 24:37.690
So we're now ready to tackle the final part, which is to make coffee.

320
24:37.780 --> 24:43.990
If the transaction is successful and there are enough resources to make the drinks a user selected,

321
24:43.990 --> 24:48.970
then the ingredients that make the drink should be deducted from the coffee machines resources.

322
24:48.970 --> 24:52.780
For example, before I purchase a latte, I have 300ml of water.

323
24:52.780 --> 24:59.890
After I purchase a latte that gets reduced by 200 to 100, and the same happens to the other ingredient

324
24:59.890 --> 25:00.550
values,

325
25:00.550 --> 25:05.710
but of course, the money goes up because I've already taken payment in the previous step.

326
25:05.860 --> 25:13.180
Let's take this into an if statement, because remember, this function returns True when the payment

327
25:13.180 --> 25:16.000
is accepted, or false if the money is insufficient.

328
25:16.000 --> 25:21.980
So this is where we're going to call our next function, which is to make coffee.

329
25:22.370 --> 25:24.920
So let's create a make_coffee().

330
25:26.390 --> 25:34.310
In order to make coffee, we need to know the drink_name so that we can tell the user here is your particular

331
25:34.310 --> 25:34.940
drink,

332
25:34.940 --> 25:39.380
and we'll also need to have the order_ingredients.

333
25:39.800 --> 25:47.960
The goal of this function is to deduct the required ingredients from the resources.

334
25:48.230 --> 25:56.120
In order to do that, we're going to get hold of the order_ingredients and we're going to loop through

335
25:56.120 --> 25:56.360
them.

336
25:56.360 --> 26:04.700
So for each of the item in the order_ingredients, we're going to look inside the resources for that

337
26:04.700 --> 26:05.930
particular item,

338
26:05.930 --> 26:10.710
and we're going to subtract the amount that's in the order_ingredients.

339
26:11.250 --> 26:16.890
And once all of that is done so the for loop ends, then we can print...

340
26:16.890 --> 26:20.070
And we can even add an emoji to this.

341
26:22.230 --> 26:27.810
On a Mac you can insert an emoji by going to Edit, Emoji & Symbols, on Windows,

342
26:27.810 --> 26:34.680
the easiest thing to do is to just search Google for a coffee emoji and then copy and paste it into

343
26:34.680 --> 26:35.370
your code.

344
26:35.550 --> 26:38.250
So now let's call make_coffee() here,

345
26:38.370 --> 26:47.850
and notice how if we are here, the machine is_on, and it's not any of these other previous choices,

346
26:47.850 --> 26:52.500
and then the resource is sufficient, and the transaction is successful.

347
26:52.500 --> 26:58.330
These are all the steps that it took us to get to this particular stage in our code.

348
26:58.360 --> 27:02.740
So now we're going to make the coffee and we're going to pass in two things.

349
27:02.740 --> 27:04.600
So let's just see the prompt again.

350
27:04.600 --> 27:11.440
We need to give the drink_name, which is going to be the choice that the user entered, and the

351
27:11.440 --> 27:14.260
order_ingredients, which is going to come from the drink,

352
27:14.260 --> 27:16.390
and it's under the key, ingredients.

353
27:17.410 --> 27:22.030
So now we're ready to test and run our code once more.

354
27:22.060 --> 27:26.620
Let's say I want a latte and I'm going to insert lots of money.

355
27:26.710 --> 27:29.350
And now I have my latte.

356
27:29.380 --> 27:36.910
If I print report, you should be able to see that a bunch of resources were subtracted, and the money

357
27:36.910 --> 27:38.620
is increased.

358
27:38.650 --> 27:45.320
If I try to order another latte, it should fail because there's not enough water and there's not enough

359
27:45.320 --> 27:45.890
milk.

360
27:46.490 --> 27:52.850
You can see that it's not going to let me go through as long as one of the ingredients is not enough.

361
27:54.110 --> 27:59.930
So we've now managed to fulfill all of the requirements of our Coffee Cachine Program.

362
27:59.960 --> 28:04.730
Now, there's probably a lot more complexity that you could add to your coffee machine,

363
28:04.730 --> 28:12.290
but basically what I wanted to show you today is how you would program something that exists in real

364
28:12.290 --> 28:18.620
life, like a coffee machine. And even something that seems as simple as a coffee machine,

365
28:18.620 --> 28:24.080
it can lead to a lot of errors, a lot of bugs, and a lot of anguish.

366
28:24.080 --> 28:25.040
But it's good.

367
28:25.040 --> 28:30.650
The more that you struggle, the closer you get towards your goals, and the clearer the role of the

368
28:30.650 --> 28:35.220
function, the easier it will be for you to untangle the logic.

369
28:36.240 --> 28:41.370
If you want to take a look at the completed code that I've written in this lesson, then you can head

370
28:41.370 --> 28:45.240
over to solution.py in the Beginner's PyCharm Course.

371
28:45.240 --> 28:51.510
Make sure that you've managed to fix any issues in your code, and that it runs exactly in the same

372
28:51.510 --> 28:55.170
way as expected in the program requirements.