-2

I am completely new to python, i'm learning it online through a course and got stuck in an exercise. I can't figure out the problem at all, i feel like my solution to the exercise is ok but whenever i choose the option of adding cheese to my "S" pizza, it does not add it in the final price even though i've written it in my code, can anyone explain me whats the problem with my solution?

I don't know if I've explained correctly but I've added a screenshot to make it easier to understand.

Any help is deeply appreciated ;-; i'm a noob and i'm sure it would be easy for pros to explain such an easy exercise's problem, halp. I can't figure out the problem.

This is the result i get when i try to add the cheese to the S pizza, the other sizes of the pizza work well, i dont understand whats the problem at all..

Terminal:-

Welcome to Python Pizza Deliveries!
What size pizza do you want? S, M, or L *S*
Do you want pepperoni? Y or N *N*
Do you want extra cheese? Y or N *Y*
Your final bill is: $15.

I've attached image so that its easier for you guys to understand.

screenshot

Here's the code:-

print("Welcome to Python Pizza Deliveries!")
size = input("What size pizza do you want? S, M, or L ")
add_pepperoni = input("Do you want pepperoni? Y or N ")
extra_cheese = input("Do you want extra cheese? Y or N ")
price = 0
if size == "L":
    price = 25
    if add_pepperoni == "Y":
        price += 3
        if extra_cheese == "Y":
            price += 1
if size == "M":
    price = 20
    if add_pepperoni == "Y":
        price += 3
        if extra_cheese == "Y":
            price += 1
if size == "S":
    price = 15
    if add_pepperoni == "Y":
        price += 2
        if extra_cheese == "Y":
            price += 1
print(f"Your final bill is: ${price}.")
Zo.A
  • 9
  • 3
  • @TomKarzes *"I had already explained what the problem was"* - Exactly! And the site has explicitly told you thousands of times [not to do that in comments](https://i.stack.imgur.com/0tiVK.png). – Kelly Bundy Jun 20 '23 at 13:12
  • @TomKarzes Useful information that you should've posted **as an answer** instead, not as a comment here. Your violation of the instructions quite possibly even **encourages** others like Hoang to violate them equally. It's a cancer. Usually I just flag such comments and ~98% of the time they get deleted soon after. Maybe ~2000 of them so far. Just sometimes you people go too far and I speak up. It's especially frustrating now that flags aren't handled due to the strike. – Kelly Bundy Jun 20 '23 at 13:30
  • @TomKarzes While I already rephrased that, I stand by it. Had even googled "cancer" beforehand. This violation of the instructions, encouraging the same bad behavior in others, is cancerous. – Kelly Bundy Jun 20 '23 at 13:37

5 Answers5

2

With so many nested if statements, you're bound to run into this kind of issue. Here's a slightly more expandable solution:

options = {
    'L': (25, 3, 1),
    'M': (20, 3, 1),
    'S': (15, 2, 1),
}

prices = options[size]
price = prices[0]

if add_pepperoni == 'Y':
    price += prices[1]
if extra_cheese == 'Y':
    price += prices[2]
  • Using a dictionary like that is a very good strategy. You could avoid indexing the tuple by unpacking it immediately from the dictionary which would also make it a little more readable – DarkKnight Jun 20 '23 at 11:27
  • This is the most readable solution in my opinion. Since the size only matters initially, not for the rest of the code. The logic will stay the same and it easier to change the prices later on as well. – Harald S. Hanssen Jun 20 '23 at 12:28
0

The issue you face is that the if statement for adding cheese is nested in the if statement for adding pepperoni. This means that adding cheese will only be executed if you also chose to add pepperoni. You can correct this by unnesting your if statements for cheese and pepperoni so that both will always be executed.

kvsm
  • 365
  • 1
  • 7
0

the if condition on the cheese is under the if condition on the pepperoni. As you have chosen not to have pepperoni, the if condition on the cheese is never triggered. Just move the if condition in by one tab

   if size == "S":
       price = 15
       if add_pepperoni == "Y":
           price += 2
       if extra_cheese == "Y":
           price += 1
0

Looking at the logic of your code, I can say that you can add cheese on a pizza only if you already have some pepperoni.

if L:
  ...
  if P:
    ...
    if C:
      ...
if M:
  ...
  if P:
    ...
    if C:
      ...
if S:
  ...
  if P:
    ...
    if C:
      ...

Is not like :

if L:
  ...
  if P:
    ...
  if C:
    ...
if M:
  ...
  if P:
    ...
  if C:
    ...
if S:
  ...
  if P:
    ...
  if C:
    ...

Note that it may not be the better design for this problem, but as begineers, we have to start somewhere and improve :-)

Have a nice journey in coding and discover algorithms logic !

0

The error is when you use:

if size == "S":
    price = 15
    if add_pepperoni == "Y":
        price += 2
        if extra_cheese == "Y":
            price += 1

If you look carefully, you only check for extra cheese if there is added pepperoni.

A better implementation:

print("Welcome to Python Pizza Deliveries!")
size = input("What size pizza do you want? S, M, or L ")
add_pepperoni = input("Do you want pepperoni? Y or N ")
extra_cheese = input("Do you want extra cheese? Y or N ")
price = 0
if size == "L":
    price = 25
    if add_pepperoni == "Y":
        price += 3
    if extra_cheese == "Y":
        price += 1
if size == "M":
    price = 20
    if add_pepperoni == "Y":
        price += 3
    if extra_cheese == "Y":
        price += 1
if size == "S":
    price = 15
    if add_pepperoni == "Y":
        price += 2
    if extra_cheese == "Y":
        price += 1
print(f"Your final bill is: ${price}.")

It's just the spacing and a common error is that, because when you create a new line, the spacing doesn't change back even if you but, say an else statement is inside an if statement, which doesn't make sense.