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

261 views Asked by At

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

There are 5 answers

0
Rom1deTroyes On BEST ANSWER

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 !

3
kvsm On

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.

1
Basilikumpel On

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
2
AFRobertson On

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]
0
AudioBubble On

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.