All language subtitles for 7. Solution & Walkthrough for the Coffee Machine Code

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese Download
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

0 Now, 1

there are so many possible solutions to create this coffee machine program 2

satisfying all the requirements. And the way that you code it up is essentially 3

your choice, right? 4

Whether if you decide to use a while loop or use a for loop or create a 5

different data structure, there are endless possibilities. 6

What I'm going to show you now is just one of those possibilities. 7

And what's really important is you don't feel like you've done it wrong 8

just because it's different to mine. As long as it works 9

the way that you expect it to, then consider yourself successful. 10

The first thing I'm going to do is create a new project in PyCharm, 11

and I'm going to call my project coffee machine. Again, 12

making sure that I've got the latest version of Python as the interpreter I'll 13

click 14

create. 15

Now that the first thing I'm going to do here is right-click on this and create 16

a new file, which is going to be my main.py. 17

And then I'm going to go to my starting project in Repl.it and I'm just going to 18

copy everything there is over here and paste it into this main.py. 19

Now you might find the font of your code or of PyCharm a little bit too big or 20

too small. If that's the case, 21

you can just go into preferences and change the appearance and the font to a 22

different size. And this is for the user interface. 23

And if you want to change the font of the editor, then you can go here, 24

go to font and then change that size here. 25

I've tried to make it as large as possible so that when you were looking at this 26

video on an iPad or an iPhone, all the code still be readable. 27

But of course, normally when you're coding, 28

you'd probably want to fit more lines into the same screen, 29

but it's not good to strain your eyes. So try to strike a balance there. 30

Now I'm going to collapse that sidebar because I'm going to be coding entirely 31

in this one file. 32

Now notice that the beginning we get given a menu, 33

and this is a dictionary which contains three entries. 34

And each of those entries have a name of a drink, 35

espresso, latte and cappuccino. 36

And then each of them have a value that holds a bunch of data, 37

including the ingredients that are required to make that drink 38

and also the price of the drink. Now there's also a resources dictionary, 39

which holds the resources of the coffee machine. 40

Now that we've got all of that, then we're ready to go. 41

I'm going to tackle these requirements one by one. 42

And the first one says to prompt the user by asking, 43

what would you like? Espresso, latte or cappuccino. 44

So I'm actually gonna just straight up copy this line and put that into an 45

input. This is going to be saved into some sort of variable, 46

which I'll name choice. 47

Now it tells me that the prompt should show every time action has completed. 48

For example, 49

once the drink is dispensed and it should show again and again and again. 50

So that means we probably going to have to embed this input in some sort of while 51

loop. So I'm just gonna say for now while something is true, 52

then keep asking for this prompt. Right now 53

if we decide to go and run this code where we go to run and then click on this 54

button and then select the main.py to run, 55

then you can see that it asks me what would you like? And if I've put an input, 56

it'll keep asking me until eternity, basically, 57

because that there's currently no way of turning that true into a false. 58

So let's take a look at the next requirement. 59

We should be able to turn off the coffee machine by entering off into the prompt. 60

For maintainers of the coffee machine, 61

they can use off as the secret word to turn off the machine and your code should 62

end execution when this happens. So when somebody wants to buy coffee, 63

this is the line that they see. But when a maintenance guy comes along, 64

then they should be able to enter something. 65

And if this choice happens to equal the secret code, 66

which is off, then in this case we should stop the while loop and exit. 67

So that gives us a way of changing this true into some other form of variable, 68

right? So we could create a new variable code is_on, 69

start it off as true and while the machine is on, 70

then it should continue to loop through and ask the user for their choice. 71

But if the choice happens to be off, then we're going to turn 72

that is_on into false. Now, 73

if we run our code again, 74

so now that you've run it once you can either stop it or you can rerun it, 75

and now it's going to stop your existing code and rerun the code. 76

So if you don't want to see this dialogue every time, 77

then just check this box and then click stop and rerun. 78

So now if we make a selection or we say something basically anything other than 79

the key word, which is off, 80

it's going to loop back and forth and keep prompting us. 81

But if I say off, then the machine turns off and you can see that I've now exited 82

the program. Now we've tackled 1 and 2. 83

Let's go on to number 3. When the user enters the keyword report to 84

the prompt, another secret word, a report 85

should be generated that shows the current resource values. For example, 86

water, milk, coffee, and money. So how can we do that? Well, firstly, 87

we don't actually have a variable that holds the amount of money. 88

So let's create something, 89

let's call it profit maybe and set it to equal zero to begin with. 90

Our machine has an empty money box in the beginning. 91

So now we have to check to see, well, elif 92

the choice was equal to report. Well, in this case, 93

we have to generate a report and the report is basically going to print all the 94

values of these resources. 95

So I'm simply going to copy the expected output and I'm going to paste it here. 96

And then we can try and turn that into print statements, 97

making it a dynamic instead of hard-coded. In my case, 98

I want to add print in front of all of these lines. 99

And previously we've been doing this just by writing it one by one, 100

and then maybe we could copy and paste it. 101

But let me show you a quick tip that you can do in PyCharm. If you're on Windows, 102

hold down the alt and the shift key on your keyboard. If you are on a Mac, 103

hold down the option and the shift key. 104

Now click at the beginning and hold and drag down. 105

So if that doesn't work, try out a few times, 106

you'll get the hang of it eventually. But notice how I've now got four cursors 107

and that means when I write print, check this out. 108

Isn't that cool? 109

I've managed to write on four lines at once because I need that repeat 110

functionality and this is a way of doing multiline editing. 111

Remember that shortcut and use it in the future if you find it useful. 112

So I'm actually going to change this all into fstrings because I want to change 113

these numbers instead of being hard-coded, 114

I want to insert them into here using the curly braces. 115

The water is stored under resources, 116

and then it's in the key called water. 117

And now notice how I've got a outer double quote, 118

so I can't have an inner double quote. 119

So I'm going to change this into single quotes instead, 120

like, so. And I'm going to do the same for milk and coffee. 121

So now I've added the water, milk, and coffee into my print statement. 122

All I have left is the money. 123

So let's delete the value and let's insert that profit into here. 124

Now let's run our code again and let's check it out. 125

If I type report it should now give me a report of all the current values and 126

money is equal to $0 because that's what we start off with. 127

We're now ready to tackle number 4. Here 128

we have to check that when the user chooses a drink, we're going to check 129

if there are enough resources to make that particular drink they chose. 130

For example, if a latte requires 200 ml of water 131

but there's only 100ml left in the machine, 132

it should not make the drink because it actually can't make the drink 133

and it's going to print out, 'Sorry, there's not enough water' or not enough milk, 134

not enough coffee, whatever it may be. 135

So let's tackle this particular checkpoint. 136

Now that I've got the if choices equal off, if choice equals report. 137

Now, if it's not either of those, 138

then they're probably going to be entering the name of a drink. 139

So let's catch that using an else statement. 140

And then inside this else statement 141

I'm going to get hold of the particular drink that they ordered by tapping into 142

our menu dictionary and then using that choice they typed in as the key. 143

Let's say that the particular drink that they chose is equal to menu 144

and then the key is of course going to be the choice. 145

So now if I just print this drink and I run my code.. 146

So the shortcut for running is actually holding down the control and R and now 147

it can go down here and you can see it's asking me for what I would like, 148

so I'm going to choose latte. And what's going to be printed 149

is the latte entry in my recipes dictionary up here, so this particular 150

value. 151

Now that I've got this value stored inside a variable called drink, 152

well then I can tap into its ingredients and loop through each of the ingredients 153

comparing it against the resources and seeing if there's enough. 154

Now this is a little bit of functionality that should probably be self- 155

contained. So instead of just printing the drink, 156

I'm actually going to create a new function. So up here, 157

I'm going to create a new function with our def and I'm going to call it is_ 158

resource_sufficient. 159

And this is_resource_efficient is going to take the order ingredients as a 160

input and then it's going to work on that. 161

So if we want to call that function and pass in the order ingredients we'll have 162

to call is_resource_efficient 163

and then the other ingredients will be from the drink and then getting hold of 164

the values under the key ingredients. 165

All right. 166

So under this particular key, it will fetch this particular dictionary. 167

And this is the dictionary that's going to be passed over to this function as 168

the input. 169

So now that we have a hold of a dictionary with all the ingredients that are 170

required and the amount of each ingredient, 171

we can now compare it against our resources 172

which is a very similar dictionary with the resources and the amount that's left 173

in the machine. 174

We can loop through the order ingredients and for each of the items in the 175

ingredients, 176

we're going to check to see if the order ingredients at that particular key, 177

so this is getting hold of the value, is greater than or equal to the resources 178

using the same particular key. For example, 179

if we were looking at the first example, 180

the item would be equal to water. 181

So if we fetch the value from order ingredients 182

with the key of water, we should get of 200. 183

And we would now test to see if 200 is greater than or equal to the 300 that 184

we have under the resources. Well, in this case, 185

then we should probably tell the user that we actually can't make it. 186

So let's put an if statement there and I'm going to use this same string here to 187

print it out. 188

Now notice when I pasted that string in and 189

it has the double quotes from the PDF file here that it's actually not being 190

recognized and I'm getting an error here. 191

And the important thing to know is that there's a difference between decorative 192

double quotes, like these, 193

which looked different for the beginning quote and the end quote. 194

And then there are Programming double quotes which look like this, 195

so I'm going to select this whole line 196

and I'm going to add a double quote and notice how they look identical from the 197

front and the back. So now it's going to print, Sorry 198

there is not enough. 199

And the enough of what? It's going to be 200

the item that we're currently looping through. 201

So let's change that to an fstring 202

which makes that an active piece of code that's going to be inserted. 203

And in this case, 204

we're going to return false because there is not enough resources. 205

But otherwise, 206

if we managed to get to the end of the for loop and we still haven't returned or 207

exited the function by returning false, then in this case 208

we can return true. 209

So if this particular logic is a little bit confusing for you, 210

you could have something like, like this. 211

So you have is_enough = true, and you could change is_enough 212

to false. 213

If any of the order ingredients are greater than the resources. 214

And finally at the end, you could return is_enough. 215

So basically it stays true unless one of these if statements gets activated. 216

But for simplicity sake, I'm actually just going to keep it simple like this. 217

And we're now ready to receive that result here 218

so we can put an if statement here. 219

If the resources are sufficient for the drink, 220

then we can proceed to continue to the next step. 221

The next step is to process coins. 222

The user's going to be asked for the number of quarters 223

they have, the number of dimes, nickels, and pennies, 224

and you have to remember their values. 225

So if you're from the US this shouldn't be a problem. But if you're like me 226

somebody who's not from the US, um, 227

I actually find it really confusing when I go to the States. 228

I always think that the larger coin, that the nickel, should be worth more than 229

the dime, but I think it was just me being silly. 230

So we're going to ask the user to insert some coins, we're going to process it 231

and then we're going to calculate the total value of the coins 232

they inserted. That, to me, 233

sounds like it should be a separate function as well. 234

So let's create another function here, which I'm going to call process_coins. 235

And this is not going to take any input, 236

but it is going to return the total value of the coins inserted. 237

Now, how do we process coins? Well, 238

first we can print to ask them to please insert coins. 239

And then afterwards, we're going to somehow calculate a total, right? 240

This is the variable that we're going to keep track of 241

and we're going to return as the output of this function. 242

The total is going to be calculated based on the four types of coins. 243

So the first question we're going to ask them is how many quarters? 244

And this of course is going to be a whole number, 245

so we're going to turn it from a string into an integer. 246

And we know that each quarter is worth 0.25 247

of a dollar, 248

so we can multiply the number of quarters by 0.25 249

and then we'll get the monetary value. 250

Now we'll need to do the same thing for a bunch of other coins. 251

So instead of quarters, this is going to be dimes, 252

and then it's going to be nickels and finally, 253

it's going to be pennies. Dimes are worth 10 cents, 254

nickels are worth 5 cents, 255

and pennies are worth 1 cent. Now, 256

instead of just setting the totals of each of these values, every subsequent one 257

other than the first one 258

which remember creates this variable and sets its value, 259

every other one is just going to be added to the current value like this. 260

So now at the very end of all of this, 261

we're going to return the total as the output. 262

And whenever you have something that returns like both of these functions, 263

you should probably be adding a docstring. So in this case, 264

this returns 265

the total calculated from coins 266

inserted. And in this case, 267

what happens is it returns true when order can be made and false 268

if ingredients are insufficient. 269

Now let's call this function that we created, process_coins. 270

If there's enough resources to make the drink, 271

then the next step is to actually ask them for the money. 272

So here is where we're going to call process coins. 273

And notice when I write this and I hover over it, you can see that docstring 274

we just wrote, returns the total from coins inserted. 275

That means this is going to return 276

and we need to capture the user's payment in this variable. 277

So this returns the output replaces this function call, 278

and then it gets saved inside this variable called payment. Now, 279

what are we going to do with this payment? Well, that goes onto the next step, 280

which is to check that the transaction was successful. 281

So we have to make sure that the user has inserted enough money to actually 282

purchase the drink they wanted. But each drink of course has a different price. 283

So if the user inserts enough money, then we're going to give them some change. 284

But if they haven't inserted enough money, then we're going to say, sorry, 285

that's not enough money. And the money is refunded. 286

But if they have inserted enough money then the cost of the drink is going to be 287

added to the machine as the profit. So the next time when we trigger the report, 288

then we're going to get to see the increase in the monetary value. 289

Again, 290

let's create a new function and let's get rid of some of these squiggly lines by 291

adding enough spaces in between the functions. 292

This one I'm going to call is_transaction_successful, 293

because that's basically what we're going to be checking. 294

And it's going to take two inputs. 295

It's going to take a input in terms of the amount of money that was received, 296

and it's also going to have another input, which is cost of the drink. 297

This function's goal is to return true when the payment is accepted or it's 298

going to return false 299

if the money is insufficient. 300

Uhm 301

Notice how there's this line to the right here of your editor. 302

Basically what happens is if you have a line of code that's a little bit too 303

long past the recommendation from PEP 8, 304

you can see that PEP 8 recommends that a line should not be longer than 120 305

characters 306

because it's very hard to read for somebody scrolling around like this. 307

So in this case, 308

they would want you to put it onto a new line so that you don't have to scroll 309

and you can see it all on the same screen. But in our case, 310

this is not a problem because we have not exceeded the line length 311

recommendation. 312

So how are you going to check if the transaction is successful? 313

Well, 314

if the money received is greater or equal to the cost of the drink, well in this case 315

that means we should return true, right? The transaction is successful. 316

And if it's not the case, if it's the opposite case, 317

then we're going to print, Sorry that's not enough money. 318

And the money is refunded to them. 319

Right. 320

So let's print that here. And we're also going to return false. 321

Remember that the return has to be the last thing in your function. 322

If you put this above the print statement then the print statement will never 323

get called, and that's why you have this highlight. And if you click on it, 324

you can see that it tells you this code is unreachable. 325

So there's a lot of these little hints and tips that really help you when you're 326

developing. 327

But if the user has inserted enough money then the cost of the drink should be 328

added to the machine as the profit so that we can see it in the next time report 329

is triggered. 330

So this means that if this money_received is greater or equal to the drink cost, 331

then we're going to add to this variable called profit that we have up here, 332

which starts out at zero, 333

but we're going to add the drinks cost to profit. 334

So we're going to say profit += drink_cost. 335

And now you'll see an error under the profit 336

because this is acting inside 337

a local scope and profit is outside in the global scope. 338

So in order to reach it, we have to say global profit. 339

And the final part of checking the transaction is seeing if the user has 340

inserted too much money then the machine is going to offer change. For example, 341

here is however many dollars in change and the change should be rounded to two 342

decimal places. Again, it's going to be inside this if statement. 343

So the change is going to be equal to the amount of money received, 344

subtracting the drink cost. 345

And this of course could be any number of decimal places. 346

So we can use the round function 347

which you've seen a long time ago to round this number 348

and the second input is the number of decimal places. 349

So if you just hover over the function name, then you can see the docs come up 350

and this function is basically going to around a number to a given precision in 351

decimal digits. So the first is the number you want to round, 352

and the second is the number of digits that you want after the dot 353

basically. Now that we've gotten hold of the change, 354

we're going to print and tell the user, 355

basically, here is this many dollars in change. 356

And of course I have to add an F to activate that fstring. 357

So now we're ready to call is_transaction_successful 358

and we're going to pass in the money_received and the drink_cost. 359

So the money_received is, of course, going to be the payment from the previous step 360

that was calculated from all the coins and the drink_cost is going to be based 361

on the drink and it's under the key cost, 362

which we can confirm up here. So the drink is this dictionary, 363

and then there's the ingredients and the cost. 364

Right. 365

Let's rerun our code and let's test it out with something. Let's say 366

I want a latte. Please insert coins. 367

Let's say we tried to insert insufficient coins. 368

It tells us, sorry, that's not enough money. Money is refunded. 369

But let's try giving it enough coins this time. 370

And in this case, 371

it accepts it and it tells us here's $2.42 in change rounded to two decimal places, 372

and we're ready to take another drink. So now if we hit report, 373

you can see that we've now earned some money in our machine and all our code is 374

working as expected. 375

So we're now ready to tackle the final part, which is to make coffee. 376

If the transaction is successful and there are enough resources to make the 377

drinks 378

the user selected, then the ingredients to make the drink should be deducted from 379

the coffee machine's resources. For example, before I purchased a latte 380

I have 300ml of water. 381

After I purchased the latte that gets reduced by 200 to 100, 382

and the same happens to the other ingredient values. 383

But of course the money goes up because I've already taken payment in the 384

previous step. Let's take this into an if statement 385

because remember this function returns true when the payment is accepted or false 386

if their money is insufficient. 387

So this is where we're going to call on next function 388

which is to make coffee. So let's create 389

make_coffee. In order to make coffee 390

we need to know the drink name so that we can tell the user here's your 391

particular drink. 392

And it will also need to have the order ingredients. 393

The goal of this function is to deduct the required ingredients from the 394

resources. In order to do that, 395

we're going to get hold of the order ingredients and we're going to loop 396

through them. So for each of the items in the order_ingredients, 397

we're going to look inside the resources for that particular item 398

and we're going to subtract the amount that's in the order_ingredients. 399

And once all of that is done, so the for loop 400

ends, then we can print and we can even add an emoji to this. 401

Right. 402

On a Mac 403

you can insert an emoji by going to edit, emoji, and symbols. On Windows 404

the easiest thing to do is to just search Google for a coffee emoji 405

and then copy and paste it into your code. 406

So now let's call make_coffee here and notice how 407

if we are here the machine is on 408

and it's not any of these other previous choices, 409

and then the resource is sufficient and the transaction is successful. 410

These are all the steps that it took us to get to this particular stage in our 411

code. 412

So now we're going to make the coffee and we're going to pass in two things. 413

So let's just see the prompt again. We need to give the drink name 414

which is going to be the choice that the user entered, and the order ingredients 415

which is going to come from the drink and it's under the key ingredients. 416

So now we're ready to test and run our code once more. 417

Let's say, I want a latte and I'm going to insert lots of money. 418

And now I have my latte. If I hit report, 419

you should be able to see that a bunch of resources were subtracted and the 420

money is increased. If I try to order another latte, 421

it should fail because there is not enough water and there's not enough milk. 422

You can see that it's not going to let me go through as long as one of the 423

ingredients is not enough. 424

So we've now managed to fulfill all of the requirements of our coffee machine 425

program. Now, 426

this probably a lot more complexity that you could add to your coffee machine, 427

but basically what I wanted to show you today is how you would program something 428

that exists in real life, 429

like a coffee machine and even something that seems as simple as a coffee 430

machine. It can lead to a lot of errors, 431

a lot of bugs and a lot of anguish, but its good. 432

The more that you struggle, 433

the closer you get towards your goals. And the clearer the role of the function, 434

the easier it will be for you to untangle the logic. 435

If you want to take a look at the completed code that I've written in this 436

lesson, 437

then simply head over to the link that's in the resources and you'll be able to 438

see it in 439

Repl.it. Make sure that you've managed to fix any issues in your code 440

and that it runs exactly the same way as expected in the program requirements.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.