#
# A program that accepts the total amount of purchase, calculates the
# appropriate discount, and displays the discount and the purchase after
# discount.


from math import *

def main():
cost = get_cost()
discount = get_discount()
% = calc_percent(discount)
final cost = get_final_cost
cost_percent = cost_discount(cost, discount)
show_result(cost_discount_final_cost)


def get_cost():
return float(raw_input("Enter the cost before discount: "))


def get_discount():
return float(raw_input("Enter your discount is: "))


def calc_percent(discount)
% = 0.10 on the entire purchase if it exceeds $1,000.00
% = 0.05 on the entire purchase if it exceeds $500.00, but not $1,000.00
print "No discount on puchases of $500.00 or less

:o

Dani AI

Generated

Good start — the idea is exactly what and pointed out: compute the discount from the purchase total (no need to ask the user for a discount), and use simple conditionals. Below is a compact, more robust pattern that avoids float rounding errors, validates input, and cleanly separates rate calculation from presentation.

from decimal import Decimal, ROUND_HALF_UP

def discount_rate(cost):
    if cost > Decimal('1000.00'):
        return Decimal('0.10')
    if cost > Decimal('500.00'):
        return Decimal('0.05')
    return Decimal('0.00')

def apply_discount(cost):
    rate = discount_rate(cost)
    discount = (cost * rate).quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)
    total = (cost - discount).quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)
    return rate, discount, total
if __name__ == '__main__':
    from decimal import Decimal, InvalidOperation
    while True:
        s = input('Enter purchase amount: ').strip().replace(',', '').replace('$', '')
        try:
            cost = Decimal(s)
            if cost < 0:
                raise ValueError
            break
        except (InvalidOperation, ValueError):
            print('Please enter a non-negative numeric amount, e.g. 1250.75')

    rate, disc, total = apply_discount(cost)
    print(f'Discount rate: {int(rate * 100)}%')
    print(f'Discount amount: ${disc}')
    print(f'Total after discount: ${total}')

Quick tips: use Decimal for money to avoid float surprises; sanitize input (strip commas or $ signs); don’t use % as a variable name; and put logic behind if __name__ == '__main__' so functions can be tested or imported. Expected results: 1200 -> 10% ($120.00 off), 750 -> 5% ($37.50 off), 400 -> no discount.

Recommended Answers

All 4 Replies

Since your discount is calculated from the total purchase, you do not need to ask for it.
Discount calculations are done using conditional if statements.

Do not use % as a variable name!

All calculations are simple, no need for "from math import *"

can you show me please i tried using using the if statements

You are almost there, please try to understand the logic here ...

def calc_discount(cost):
    if cost > 1000.00:
        discount = cost * 0.1    # 10%
        return discount
    elif cost > 500.00:
        discount = cost * 0.05   # 5%
        return discount
    else:
        # no discount
        return 0.0

Once you know the cost of the purchase then discount = calc_discount(cost) and your final cost will be cost - discount. Not really rocket science, but very simple algebra!

thank you...it helped alot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.