Hi, the program is this:

Discount problem:

Available deals:

10% off the total price when you buy $200 or more
15% off the total price when you buy $500 or more
20% off the total price when you buy $1000 or more

Write a program that outputs the value, price and savings for a customer.
Assume that the customer will get the best deal for what they buy.

Ex:
Enter the total purchased: $1500.00
Total value of goods: $1500.00
Total amount spent: $1200.00
Savings on purchase: $300.00

I don't really get how to do this program, just beginning but please don't post anything really difficult. Only learned basics and if statement and for-loop.

My code right now is:

purchased = float(raw_input("Enter the total purchased:  $"))




if purchased >= 200:
    savings = purchased / 0.10
elif purchased >= 500:
    savings = purchased / 0.15
elif purchased >= 1000:
    savings = purchased / 0.20


print "Total value of goods: $%.2f" % purchased

I don't understand how to continue, any help fast appreciated, thanks.

Hi
I think you almost have the program working correctly
. All you need to do is subtract the percent value from the total value. It will have to look something like this:

purchased  = float( raw_input( "Enter the total purchase: $" )

if purchased >= 200:
total = purchased - ( 0.10 * purchased )
elif purchased >= 500:
total = purchased - ( 0.15 * purchased )
elif purchased >= 1000:
total = purchased - ( 0.20 * purchased )

print "Total value of goods $%.2f" % ( purchased )
print "Total amount spent $%.2f" % ( total )
print "Savings on purchase $%.2f" % ( purchased - total )

It's close but it's still not perfect. Try this code:

purchased = float(raw_input("Enter the total purchased:  $"))

if purchased >= 1000:
    total = purchased - ( 0.20 * purchased )
elif purchased >= 500:
    total = purchased - ( 0.15 * purchased )
elif purchased >= 200:
    total = purchased - ( 0.10 * purchased )

print "Total value of goods $%.2f" % ( purchased )
print "Total amount spent $%.2f" % ( total )
print "Savings on purchase $%.2f" % ( purchased - total )

It will give the expected outcome because of the way the if statements are set up. With the original, if you put in 1500, it will give back 1350 because the code will see that the first if statement is true (1500 is greater than 200) then skip all the rest. Another way you could do this is to test to make sure it is between numbers. So

elif purchased >= 500:

becomes

elif purchased >= 500 and pruchased<1000:
Comments
very good point

Thank you both.

I want to ask, what should I add to this code now if I wanted the program to do something like this:

Enter the total purchased: $185
Total value of goods $185.00
Total amount spent $185.00
Savings on purchase $0.00
You could add another $15.00 worth of merchandise and save $5.00.

Enter the total purchased: $180.00
Total value of goods: $180.00
Total amount spent : $180.00
Savings on purchase : $0.00
You could add another $20.00 worth of merchandise for free.


Thank you again!

Edited 7 Years Ago by saikeraku: n/a

First off you're generating the discounts wrong, and secondly you're not printing the right variable, thirdly you need to start the if statment with the highest value($1000) otherwise it will be caught by the "if purchased >= 200:" if statement be cause it IS larger than 200. Try this:

purchased = float(raw_input("Enter the total purchased:  $"))




if purchased >= 1000:
    savings = purchased * 0.20
    total=purchased-savings
elif purchased >= 500:
    savings = purchased * 0.15
    total=purchased-savings
elif purchased >= 200:
    savings = purchased * 0.10
    total=purchased-savings


print "Total value of goods: $%.2f" % total

Editor's note: See winmic's earlier post

Edited 7 Years Ago by vegaseat: there was a similar earlier post

Oh okay, thanks.

Back to my other question, anyone know what I have to add to the code to output something like:

Enter the total purchased: $185
Total value of goods $185.00
Total amount spent $185.00
Savings on purchase $0.00
You could add another $15.00 worth of merchandise and save $5.00.

Enter the total purchased: $180.00
Total value of goods: $180.00
Total amount spent : $180.00
Savings on purchase : $0.00
You could add another $20.00 worth of merchandise for free.

Thanks.

ov3rcl0ck yeah that's the correct way to do it, I forgot about the if..elif stuff... sry
As for the second part thats what I came up with. You could try:

purchased = float(raw_input("Enter the total purchased:  $"))
total, addDollars = 0, 0
totalSaved = -1 

if purchased >= 1000:
    savings = purchased * 0.20
    total = purchased-savings
elif purchased >= 500:
    savings = purchased * 0.15
    total = purchased - savings
elif purchased >= 200:
    savings = purchased * 0.10
    total = purchased - savings
else:
    savings = 0
    total = purchased

# You need to calculate the total savings if the money spent are exactly 200, 500 or 1000 $ and then for each one check the corresponding purchase value. At the end just subtract the additional $ from the total savings.......sorry if the explanation sux :)

if purchased < 200:
    addDollars = 200 - purchased # get the additional dollars
    savings2 = 200 * 0.10 # find savings if purchased is 200
    totalSaved = savings2 - addDollars # subtract from total savings                for $200

elif purchased < 500:
    addDollars = 500 - purchased
    savings2 = 500 * 0.15
    totalSaved = savings2 - addDollars
elif purchased < 1000:
    addDollars = 1000 - purchased
    savings2 = 1000 * 0.15
    totalSaved = savings2 - addDollars
    
print "Total value of goods $%.2f" % ( purchased )
print "Total amount spent $%.2f" % ( total )
print "Savings on purchase $%.2f" % ( savings )

if totalSaved == 0:
    print "You could add another $%.2f worth of merchandise for free." % ( addDollars )
elif totalSaved > 0:    
    print "You could add another $%.2f worth of merchandise and save $%.2f." % ( addDollars, totalSaved )

well hope that helps. There may be incorrect stuff in the code so if you find something please post it up here :)

This article has been dead for over six months. Start a new discussion instead.