pythonif-statementinputadditionnested-if

Can someone tell me what's wrong with this code? It's beginner level


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}.")

Solution

  • 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 !