Peter Liston
Peter Liston

Reputation: 53

using if-elif-else in python to create a discount calculator

Im very sorry im so new to this and this is all very confusing to me. I searched the questions and there doesnt seem to be an answer i am looking for. Im happy with everything up to the point where it asks how many bags you've bought. I would like the programme to determine the rate of discount to be applied. Then i would like the program to calculate the mount due; the rate of discount to be applied and the final amount to be paid by the customer. This is really frustrating me because im not sure where to start. I think the best way to go about it is by using if-elif-else but obviously i could be very wrong. Any help would be seriously appreciated.

print "Bags Discount Calculator"
print "Please follow he on screen instructions"
print "--------------------------------------- "
print "Rate of Discount based on the following:"
print "____________________________________________"
print "Number of Bags:            Rate of Discount:"
print "____________________________________________"
print "Less than 25                       0%"
print "From 25-49                         5%"
print "From 50-99                         10%"
print "100 or more                        20%"
print "_____________________________________________"
password=raw_input ("Please enter your password:")
if (password=="di8count"):
print ("\n\tPasssword accepted")
numberofbags=raw_input ("Enter number of bags purchasd:")

if (numberofbags<25):
     discountRate = .05




else:
    print("\n\tIncorrect Password. Please try again.")  

Upvotes: 0

Views: 4493

Answers (4)

htrhrthert
htrhrthert

Reputation: 1

calculate_discount(nbags, price):
    if nbags < 25:
        discount = 0
    elif 25 <= nbags < 50:
        discount = 0.05
    elif 50 <= nbags < 100:
        discount = 0.1
    else:
        discount = 0.2
    amount_discounted = discount * price
    final_price = price - amount_discounted
    return final_price

Upvotes: 0

Anurag Goel
Anurag Goel

Reputation: 468

print "Bags Discount Calculator"
print "Please follow he on screen instructions"
print "--------------------------------------- "
print "Rate of Discount based on the following:"
print "____________________________________________"
print "Number of Bags:            Rate of Discount:"
print "____________________________________________"
print "Less than 25                       0%"
print "From 25-49                         5%"
print "From 50-99                         10%"
print "100 or more                        20%"
print "_____________________________________________"
password=raw_input ("Please enter your password:")
if (password=="di8count"):
    print ("\n\tPasssword accepted")
    numberofbags=input ("Enter number of bags purchasd:")


    def fun(dis):
        print "rate of discount : "+str(dis*100)+"%"
        print "price : "+str(numberofbags*5)
        print "discount received : "+str(dis*numberofbags*5)
        print "total amount due : "+str((numberofbags*5)-(dis*numberofbags*5))

    if (numberofbags<25):
        fun(0)
    elif (numberofbags<50):
        fun(0.05)
    elif (numberofbags<100):
        fun(0.1)
    else:
        fun(0.2)


else:
    print("\n\tIncorrect Password. Please try again.")

Upvotes: 1

Lobsterman
Lobsterman

Reputation: 256

What you want to do is create a function, say calculate_discount and have it do the work for you. This way it is reusable for when you want to call it at other places in your code. The function would take the number of bags as the argument and the full price of the product and calculate how much discount is to be applied and return the discounted price. Something like this should work:

def calculate_discount(nbags, price):
    if nbags < 25:
        discount = 0
    elif 25 <= nbags < 50:
        discount = 0.05
    elif 50 <= nbags < 100:
        discount = 0.1
    else:
        discount = 0.2
amount_discounted = discount * price
final_price = price - amount_discounted
return final_price

Upvotes: 1

ISanych
ISanych

Reputation: 22710

print "Bags Discount Calculator"
print "Please follow he on screen instructions"
print "--------------------------------------- "
print "Rate of Discount based on the following:"
print "____________________________________________"
print "Number of Bags:            Rate of Discount:"
print "____________________________________________"
print "Less than 25                       0%"
print "From 25-49                         5%"
print "From 50-99                         10%"
print "100 or more                        20%"
print "_____________________________________________"
password=raw_input ("Please enter your password:")
if (password=="di8count"):
    print ("\n\tPasssword accepted")
    numberofbags=raw_input ("Enter number of bags purchasd:")

    if (numberofbags<25):
        discountRate = 0
    elif (numberofbags<50):
        discountRate = 0.05
    elif (numberofbags<100):
        discountRate = 0.1
    else:
        discountRate = 0.2
else:
    print("\n\tIncorrect Password. Please try again.")

Upvotes: 0

Related Questions