All right, guys, in this lesson, I have a small coding challenge for you.
The goal is to create a Python Pizza Delivery Program, and the program is going to automatically calculate
the bill for the user based on a number of options.
So the user is going to be asked three questions in the form of an input.
And we can run through this program as it is just to see that in action.
So firstly it says, "Welcome to Python Pizza Deliveries!"
"What size pizza do you want?"
And I'm going to choose a large pizza.
So it asks me S, M or L?
So L. "Do you want pepperoni on your pizza?"
Yes, always.
And then it asks, "Do you want extra cheese?"
I'll type N for no.
So these are my inputs which will get saved into these variables:
size, pepperoni, and extra_cheese.
And what I want to happen is for us to be able to print out the final bill, saying, "Your final bill
is..." this amount, and it should be $28 based on the pricing of Python Pizza.
So you can see in the description box here we've got all the prices for the Small, Medium, and Large pizzas as
well as the modifiers.
So if they want pepperoni on a small pizza, that's an extra $2.
If they want pepperoni on a medium or large pizza, that's an extra $3.
And finally, if they want extra cheese, that's $1 on top of their bill.
So your goal is to use what you've learned about if statements, elif statements, else statements,
nested if statements,
multiple if statements,
everything you've learned so far, basically in today's lessons, to be able to build this and get it
to do what we expect it to do.
So pause the video, have a think about how you might tackle this problem, and work through each of
the TODO's in order.
Afterwards, come back and we'll go through the solution together.
By the way, in this lesson, you can click on the check button,
once you think your code is doing what it's supposed to, and we'll run through the automatic test using
various options that we've tried for size pepperoni and extra cheese to verify that your code works.
So pause the video now.
All right, so let's run through the solution together.
The first thing we want to do is to work out how much they need to pay based on their size choice.
So we've got all the prices here.
And we're going to start off with a variable called bill, that's going to start out at zero.
And we're going to check the variable size.
So if the size that they chose is equal to "S", that means they wanted a small pizza,
so then to the bill we will simply add $15.
Now at this point, because this is the very first if statement.
And we're simply setting the bill depending on the size that they chose, you might have done it differently.
You might have done this, you might have said, let's set the bill to equal 15 instead of plus equals.
Both of them work and both of them are acceptable solutions.
Now I'm going to stick to using the +=, because we're going to be using it a lot in this program.
So hopefully you already understand that this is the same as saying plus 15 to the previous value of
bill and set it to the new value.
So the next thing we need to check is what if they chose a medium sized pizza?
So we're going to use elif and we're going to check the second condition.
If size that they chose is equal to M, in this case we're going to add $20 to the bill.
So we're going to say bill += 20.
And the final version,
so the L, if they chose a large pizza we need to set it to $25.
Now, there's two options here.
You might have done an else statement because we've already tackled S and M.
The last one is just L.
So we could simply set the bill += 25.
But you might have also added a another elif statement.
So again both of these options work.
We can say elif size == "L". Depends on what you want to choose,
but both of these options will work just fine.
Now you might also want to add a final else statement if you are really careful,
because what if they typed something wrong?
What if they typed instead of S, M, and L, they typed some mumbo jumbo?
Well, that would be tackled in the else statement, and you could print out, "You typed the wrong inputs,"
just to catch that situation.
Now I have a warning sign under my print statement, and it tells me that my indentation is not a multiple
of four.
So just in case this happens to you, just make sure that every time you use an indent that you've got
four spaces in there.
So if I just add one more space and make sure that all of these are on the same indentation line, that
warning goes away.
So we're done with the first to do.
We've worked out how much to add to the build based on the user's size choice.
The next thing to do is to figure out if they chose any pepperoni.
So if pepperoni is equal to Y, then in that case we need to add some money to their bill.
But how much?
Well, it depends on the size of their previous choice.
So if they had a small pizza, it's $2.
And if they had a large pizza it's $3.
So the easiest way to do this is to use a nested if statement.
So inside the previous if statement, we've already checked to make sure that they chose Y for pepperoni.
So yes to pepperoni if we add another if statement here, this will only be checked if they already
said they wanted pepperoni.
So if the size is equal to S, then this is the case of a pepperoni on a small sized pizza.
So to the bill we can add $2 now. Else,
so they chose, "Yes" to pepperoni, but they have a different size.
Then we can say the bill should add equal $3, and that solves the pepperoni problem with a nested if
statement inside another if statement.
So the final to do is to work out their final amount based on whether if they want extra cheese.
So this is a situation where we have multiple if statements.
So we have an if statement here that's being checked.
We have an if statement here that's being checked.
And finally we have an if statement that checks if they want extra cheese.
So if this is equal to yes, then all we need to do is, add the $1 to the bill.
And that is all we need to do to complete this last, addition.
And now we can finally print out the value where we say, "Your final bill is..." and then colon, and then
a dollar sign.
And then we're going to use an f-string to insert the amount for our bill right here.
And finally we need a full stop.
Now make sure that you've actually got everything written exactly as I have, because otherwise the
checks are not going to pass.
So now that I've made sure I've got all the right full stops and, all the right wording, and hopefully
my calculations are correct, I can now click on check and if everything is correct then it tells me
congratulations, everything's correct.
So hopefully that wasn't too hard and you managed to figure it out by yourself.
If not, this is the time to go back and fix your code based on what you've learned in the solutions.
We all make mistakes and we all need help to figure out things.
There's nothing wrong with learning from the solution.
Walkthrough.
As long as you understand everything that's going on.
So go ahead and do that if you need to.
Otherwise, head over to the next lesson where we're going to learn about logical operators.