Python Forum
Help with an exercise
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with an exercise
#1
I am doing some code for a project at school and I cant for the life of me get it to do what I want it to. I want the sales number for any sales under 10 that doesn't get a discount to only display the final price like this

Output:
Enter number of packages you want to buy: 8 Your final total is $ 792.00
but it's displaying it like this

Output:
Enter number of packages you want to buy: 8 Your discount is $ 0.00 Your final total is $ 792.00
Here is my code. Can someone point me in the right direction as to how to display anything under 10 without the discount line and anything 10 and over with the discount line


packages = int(input("Enter number of packages you want to buy: "))

price = 99

packagePrice = packages * 99

if packages < 10:
    discount = 0
elif packages < 20:
    discount = 0.10
elif packages < 50:
    discount =0.20
elif packages < 100:
    discount = 0.30
else:
    discount = 0.40

packageTotal = packages * 99
discountTotal = discount * packageTotal
finalTotal = packageTotal - discountTotal

print()
print(format("Your discount is", "20s"), "$", format (discountTotal, "12,.2f"))
print(format("Your final total is", "20s"), "$", format (finalTotal, "12,.2f"))
Reply
#2
You just need to put the printing of the discount under if discount:.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thank you that fixed that issue. I have 1 more question. If I wanted to prevent the program from crashing if someone entered anything but a whole number how would I go about routing the error message to say something like "You can only order whole packages"
Reply
#4
What have you tried? How is it not working?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
I have tried this and it's working if someone enters a float but not if someone enters a 0 or a negative number. I want it to also error and tell them they need to buy at least 1 package if they enter 0 or a negative number.

while True:
    try:
        packages = int(input("Please enter the amount of packages you want to buy: "))
    except ValueError:
        print("Please enter a number whole number above 0.")
        continue
    else:
        break


#packages = int(input("Enter number of packages you want to buy: "))

price = 99

if packages < 10:
    discount = 0
elif packages < 20:
    discount = 0.10
elif packages < 50:
    discount =0.20
elif packages < 100:
    discount = 0.30
else:
    discount = 0.40

packageTotal = packages * 99
discountTotal = discount * packageTotal
finalTotal = packageTotal - discountTotal

print()
if discount:
    print(format("Your discount is", "20s"), "$", format (discountTotal, "12,.2f"))
    
print(format("Your final total is", "20s"), "$", format (finalTotal, "12,.2f"))
I get this output if its a 0 or -1


Output:
Please enter the amount of packages you want to buy: 0 Your final total is $ 0.00
Output:
Please enter the amount of packages you want to buy: -1 Your final total is $ -99.00
Reply
#6
You can add a test that the number is positive before the break, just like you added a test for a discount before the printing of the discount.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
write it this way:
while True:
    try:
        packages = int(input("Please enter the amount of packages you want to buy: "))
        break
    except ValueError:
        print("Please enter a number whole number above 0.")
        continue
print(packages)
Reply
#8
This is what I have now and it seems to do everything I wanted it to. Thanks for all the help

while True:
    try:
        packages = int(input("Please enter the amount of packages you want to buy: "))
        if packages < 1:  # if not a positive int print message and ask for input again
            print("Sorry, you must buy at least 1 package, try again")
            continue
    except ValueError:
        print("Please enter a number whole number above 0.")
        continue
    else:
        break

price = 99

# If's

if packages < 10:
    discount = 0
elif packages < 20:
    discount = 0.10
elif packages < 50:
    discount =0.20
elif packages < 100:
    discount = 0.30
else:
    discount = 0.40

packageTotal = packages * 99
discountTotal = discount * packageTotal
finalTotal = packageTotal - discountTotal

print()
if discount:
    print(format("Your discount is", "20s"), "$", format (discountTotal, "12,.2f"))
    
print(format("Your final total is", "20s"), "$", format (finalTotal, "12,.2f"))
Reply
#9
I don't like having the break all the way at the bottom of the loop.  But I also like to try to avoid break entirely if I can, as it can make the intent of what you're trying to do much clearer if you use variables (I try to reserve while True loops for things that I actually want to run more than once, instead of just checking for valid input).

packages = None
valid_input = False
while not valid_input:
    try:
        packages = int(input("Num packages? "))
        valid_input = packages >= 1
        if not valid_input:
            print("At least one, yo")
    except ValueError:
        print("Numbers only, no commas or other formatting is permitted.")

# now packages is a positive integer
print(packages)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020