WEBVTT

0
00:00.290 --> 00:04.400
All right, guys, in this lesson, I have a small coding challenge for you.

1
00:04.430 --> 00:13.490
The goal is to create a Python Pizza Delivery Program, and the program is going to automatically calculate

2
00:13.490 --> 00:18.350
the bill for the user based on a number of options.

3
00:18.350 --> 00:23.120
So the user is going to be asked three questions in the form of an input.

4
00:23.120 --> 00:27.830
And we can run through this program as it is just to see that in action.

5
00:28.010 --> 00:32.260
So firstly it says, "Welcome to Python Pizza Deliveries!"

6
00:32.260 --> 00:34.120
"What size pizza do you want?"

7
00:34.120 --> 00:36.940
And I'm going to choose a large pizza.

8
00:36.940 --> 00:39.700
So it asks me S, M or L?

9
00:39.700 --> 00:43.330
So L. "Do you want pepperoni on your pizza?"

10
00:43.330 --> 00:44.920
Yes, always.

11
00:45.580 --> 00:48.070
And then it asks, "Do you want extra cheese?"

12
00:48.070 --> 00:49.870
I'll type N for no.

13
00:49.870 --> 00:55.410
So these are my inputs which will get saved into these variables:

14
00:55.410 --> 00:58.110
size, pepperoni, and extra_cheese.

15
00:58.200 --> 01:06.180
And what I want to happen is for us to be able to print out the final bill, saying, "Your final bill

16
01:06.180 --> 01:12.900
is..." this amount, and it should be $28 based on the pricing of Python Pizza.

17
01:12.900 --> 01:19.880
So you can see in the description box here we've got all the prices for the Small, Medium, and Large pizzas as

18
01:19.880 --> 01:21.620
well as the modifiers.

19
01:21.620 --> 01:26.270
So if they want pepperoni on a small pizza, that's an extra $2.

20
01:26.270 --> 01:31.580
If they want pepperoni on a medium or large pizza, that's an extra $3.

21
01:31.580 --> 01:36.380
And finally, if they want extra cheese, that's $1 on top of their bill.

22
01:36.950 --> 01:43.880
So your goal is to use what you've learned about if statements, elif statements, else statements,

23
01:43.910 --> 01:45.590
nested if statements,

24
01:45.590 --> 01:47.240
multiple if statements,

25
01:47.240 --> 01:52.730
everything you've learned so far, basically in today's lessons, to be able to build this and get it

26
01:52.730 --> 01:55.760
to do what we expect it to do.

27
01:55.760 --> 02:02.240
So pause the video, have a think about how you might tackle this problem, and work through each of

28
02:02.240 --> 02:04.070
the TODO's in order.

29
02:04.070 --> 02:08.120
Afterwards, come back and we'll go through the solution together.

30
02:08.140 --> 02:11.800
By the way, in this lesson, you can click on the check button,

31
02:11.800 --> 02:18.610
once you think your code is doing what it's supposed to, and we'll run through the automatic test using

32
02:18.610 --> 02:24.790
various options that we've tried for size pepperoni and extra cheese to verify that your code works.

33
02:24.790 --> 02:26.410
So pause the video now.

34
02:36.000 --> 02:38.820
All right, so let's run through the solution together.

35
02:38.820 --> 02:44.340
The first thing we want to do is to work out how much they need to pay based on their size choice.

36
02:44.340 --> 02:46.590
So we've got all the prices here.

37
02:46.590 --> 02:52.410
And we're going to start off with a variable called bill, that's going to start out at zero.

38
02:52.410 --> 02:55.770
And we're going to check the variable size.

39
02:55.770 --> 03:02.630
So if the size that they chose is equal to "S", that means they wanted a small pizza,

40
03:02.630 --> 03:08.180
so then to the bill we will simply add $15.

41
03:08.180 --> 03:11.480
Now at this point, because this is the very first if statement.

42
03:11.480 --> 03:18.200
And we're simply setting the bill depending on the size that they chose, you might have done it differently.

43
03:18.200 --> 03:24.350
You might have done this, you might have said, let's set the bill to equal 15 instead of plus equals.

44
03:24.350 --> 03:28.250
Both of them work and both of them are acceptable solutions.

45
03:28.250 --> 03:34.610
Now I'm going to stick to using the +=, because we're going to be using it a lot in this program.

46
03:34.610 --> 03:40.940
So hopefully you already understand that this is the same as saying plus 15 to the previous value of

47
03:40.940 --> 03:43.760
bill and set it to the new value.

48
03:43.760 --> 03:48.300
So the next thing we need to check is what if they chose a medium sized pizza?

49
03:48.300 --> 03:52.440
So we're going to use elif and we're going to check the second condition.

50
03:52.440 --> 04:00.450
If size that they chose is equal to M, in this case we're going to add $20 to the bill.

51
04:00.450 --> 04:03.960
So we're going to say bill += 20.

52
04:03.960 --> 04:06.090
And the final version,

53
04:06.090 --> 04:12.170
so the L, if they chose a large pizza we need to set it to $25.

54
04:12.170 --> 04:13.970
Now, there's two options here.

55
04:13.970 --> 04:19.010
You might have done an else statement because we've already tackled S and M.

56
04:19.010 --> 04:20.930
The last one is just L.

57
04:20.930 --> 04:27.860
So we could simply set the bill += 25.

58
04:28.100 --> 04:32.180
But you might have also added a another elif statement.

59
04:32.180 --> 04:34.430
So again both of these options work.

60
04:34.430 --> 04:40.370
We can say elif size == "L". Depends on what you want to choose,

61
04:40.370 --> 04:43.700
but both of these options will work just fine.

62
04:44.840 --> 04:50.750
Now you might also want to add a final else statement if you are really careful,

63
04:50.750 --> 04:53.390
because what if they typed something wrong?

64
04:53.390 --> 04:58.490
What if they typed instead of S, M, and L, they typed some mumbo jumbo?

65
04:58.520 --> 05:06.520
Well, that would be tackled in the else statement, and you could print out, "You typed the wrong inputs,"

66
05:06.970 --> 05:09.340
just to catch that situation.

67
05:09.370 --> 05:17.530
Now I have a warning sign under my print statement, and it tells me that my indentation is not a multiple

68
05:17.530 --> 05:18.160
of four.

69
05:18.160 --> 05:23.710
So just in case this happens to you, just make sure that every time you use an indent that you've got

70
05:23.710 --> 05:25.480
four spaces in there.

71
05:25.480 --> 05:31.710
So if I just add one more space and make sure that all of these are on the same indentation line, that

72
05:31.710 --> 05:32.910
warning goes away.

73
05:32.910 --> 05:34.950
So we're done with the first to do.

74
05:34.980 --> 05:39.390
We've worked out how much to add to the build based on the user's size choice.

75
05:39.390 --> 05:43.380
The next thing to do is to figure out if they chose any pepperoni.

76
05:43.380 --> 05:50.600
So if pepperoni is equal to Y, then in that case we need to add some money to their bill.

77
05:50.600 --> 05:51.950
But how much?

78
05:51.980 --> 05:55.880
Well, it depends on the size of their previous choice.

79
05:55.880 --> 05:59.540
So if they had a small pizza, it's $2.

80
05:59.540 --> 06:02.060
And if they had a large pizza it's $3.

81
06:02.060 --> 06:05.960
So the easiest way to do this is to use a nested if statement.

82
06:06.500 --> 06:12.860
So inside the previous if statement, we've already checked to make sure that they chose Y for pepperoni.

83
06:12.860 --> 06:18.910
So yes to pepperoni if we add another if statement here, this will only be checked if they already

84
06:18.910 --> 06:20.560
said they wanted pepperoni.

85
06:20.560 --> 06:28.570
So if the size is equal to S, then this is the case of a pepperoni on a small sized pizza.

86
06:28.570 --> 06:33.760
So to the bill we can add $2 now. Else,

87
06:33.760 --> 06:37.450
so they chose, "Yes" to pepperoni, but they have a different size.

88
06:37.450 --> 06:46.830
Then we can say the bill should add equal $3, and that solves the pepperoni problem with a nested if

89
06:46.830 --> 06:49.950
statement inside another if statement.

90
06:50.190 --> 06:56.070
So the final to do is to work out their final amount based on whether if they want extra cheese.

91
06:56.070 --> 07:01.290
So this is a situation where we have multiple if statements.

92
07:01.290 --> 07:03.630
So we have an if statement here that's being checked.

93
07:03.630 --> 07:06.140
We have an if statement here that's being checked.

94
07:06.140 --> 07:10.520
And finally we have an if statement that checks if they want extra cheese.

95
07:10.520 --> 07:20.810
So if this is equal to yes, then all we need to do is, add the $1 to the bill.

96
07:20.810 --> 07:26.750
And that is all we need to do to complete this last, addition.

97
07:26.750 --> 07:38.030
And now we can finally print out the value where we say, "Your final bill is..." and then colon, and then

98
07:38.030 --> 07:39.080
a dollar sign.

99
07:39.080 --> 07:45.740
And then we're going to use an f-string to insert the amount for our bill right here.

100
07:45.740 --> 07:49.490
And finally we need a full stop.

101
07:49.520 --> 07:56.200
Now make sure that you've actually got everything written exactly as I have, because otherwise the

102
07:56.200 --> 07:58.150
checks are not going to pass.

103
07:58.150 --> 08:06.490
So now that I've made sure I've got all the right full stops and, all the right wording, and hopefully

104
08:06.490 --> 08:13.090
my calculations are correct, I can now click on check and if everything is correct then it tells me

105
08:13.090 --> 08:15.250
congratulations, everything's correct.

106
08:15.910 --> 08:20.440
So hopefully that wasn't too hard and you managed to figure it out by yourself.

107
08:20.440 --> 08:26.380
If not, this is the time to go back and fix your code based on what you've learned in the solutions.

108
08:26.380 --> 08:29.710
We all make mistakes and we all need help to figure out things.

109
08:29.710 --> 08:33.880
There's nothing wrong with learning from the solution.

110
08:33.880 --> 08:34.720
Walkthrough.

111
08:34.720 --> 08:37.570
As long as you understand everything that's going on.

112
08:37.570 --> 08:39.850
So go ahead and do that if you need to.

113
08:39.850 --> 08:44.200
Otherwise, head over to the next lesson where we're going to learn about logical operators.