0

To give you some context, the project I'm doing is creating a bill based on inputs from the user, and part of the project outlines a discount if the user would like to pay within 10 days.

Our teacher said we HAVE to nest if statements in our project but I'm not sure why or how.

I missed the nesting lesson and I have no idea how to sucsessfully implement an if statement and everything I can see online is way above my skill level, and I dont see where I'm going wrong with my code.

#finding out the potential discount for paying within 10 days

if pay == "no":
    discount = 0

    if pay == "yes" and firstBill > 100 and firstBill < 200:
        discount = (firstBill * 0.015)

    elif pay == "yes" and firstBill > 200 and firstBill < 400:
        discount = (firstBill * 0.03)

    elif pay == "yes" and firstBill > 400 and firstBill < 800:
        discount = (firstBill * 0.04)

    elif  pay == "yes" and firstBill > 800:
        discount = (firstBill * 0.05)

    else:
        print("error")

else:
    print("error")
5
  • I think they want you to do one if pay == "yes" under the == "no" check. That way you don't need to constantly check if it's equal to yes. Commented Dec 8, 2018 at 2:10
  • Please note that your indentation is wrong. The way your code is written now, the second ìf happens when the first one happens, which makes no sense your your problem Commented Dec 8, 2018 at 2:11
  • @Carcigenicate I'm not sure I understand what you mean completley. My thought process (abeit probably flawed) is that if the first statement is == "no" , then it wont execute the rest of the statements if the user inputs "no", thus not creating anymore work for itself Commented Dec 8, 2018 at 2:18
  • @BrenRB Yes. Notice how many times you're checking if pay =="yes" though. You could do that check once and skip all the firstBill checks if it isn't "yes". Commented Dec 8, 2018 at 2:20
  • @Carcigenicate Ohh I see what you mean. That is much more efficient. Thank you Commented Dec 8, 2018 at 2:22

1 Answer 1

2

Do you mean something like this? The first if checks if pay is "no" and skips the rest of the code. Everything under elif pay == "yes": will only execute if pay is "yes".

if pay == "no":
    discount = 0
elif pay == "yes":
    if 100 <= firstBill < 200:
        discount = (firstBill * 0.015)
    elif 200 <= firstBill < 400:
        discount = (firstBill * 0.03)
    elif 400 <= firstBill < 800:
        discount = (firstBill * 0.04)
    elif firstBill >= 800:
        discount = (firstBill * 0.05)
    else:
        print("error")
else:
    print("error")

By the way, you can chain comparison operators like x < y < z. Also, your code prints "error" for EXACTLY 200 or EXACTLY 400 and so on. I'm assuming that was unintended and fixed that.

You can also write the if statements differently:

if pay == "yes":
    if 100 <= firstBill < 200:
        discount = (firstBill * 0.015)
    elif 200 <= firstBill < 400:
        discount = (firstBill * 0.03)
    elif 400 <= firstBill < 800:
        discount = (firstBill * 0.04)
    elif firstBill >= 800:
        discount = (firstBill * 0.05)
    else:
        print("error")
elif pay == "no":
    discount = 0
else:
    print("error")

And it will work exactly the same.

Sign up to request clarification or add additional context in comments.

3 Comments

Just ran this and it works! Thanks a ton. Just for future reference, an if statement can only be nested within an elif for it to work?
@BrenRB No, if statements can be placed essentially anywhere. I'll add another example to my answer to demo this.
@BrenRB No problem. If my answer solved your problem, please accept it for future browsers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.