Wriring a code in python for profitability

Viewed 84

I need to write a program in Python using def to calculate if it would be profitable to buy a customer card for a discount or not (meaning that the purchase cost after discount + card price is less or equal to the original cost) with the following arguments: total purchase amount in euros, discount percent with customer card and customer card price. If the card saves the customer some x amount of euros then the function should return x. If purchase + card price is more expensive than the original purchase price then return -x, where x is the number of euros it’s cheaper with the card. The output should be something like that: Total purchase amount: 200 Discount percent: 5 Customer card price: 5 It’s better to get a card, you will save 5 euros So far I manage to write this code but I don't know what to do next:

def customer_card_discount1(total_purchase_amount1, discount_percent1, customer_card_price1):
    new_price = (total_purchase amount1) * (100 - discount_percent1)/100 + 
    (customer_card_price1)
    if new_price <= total_purchase amount1:
    return customer_card_price1 - new_price
    elif new_price > total_purchase amount1:
    return customer_card_price1 - new_price
try:
    total_purchase_amount1 - int(input("Total purchase amount"))
    discount_percent1 - int(input("Discount percent"))
    customer_card_price1 - int(input("Customer card price"))
3 Answers

You may have overthought this. If I understood it correctly, all you need is to compare the original price (without any discounts) with the new price (original price, plus card price minus customer discount). This leads to a much simpler solution:

def customer_card_discount1(total_purchase_amount1, discount_percent1, customer_card_price1):
    new_price = (total_purchase_amount1) * (100 - discount_percent1)/100 + customer_card_price1
    return total_purchase_amount1 - new_price

total_purchase_amount1 = int(input("Total purchase amount: "))
discount_percent1 = int(input("Discount percent: "))
customer_card_price1 = int(input("Customer card price: "))

print(customer_card_discount1(total_purchase_amount1, discount_percent1, customer_card_price1))

Example

  • Input: 200 / 5 / 5 -> Output: 5
  • Input: 200 / 5 / 15 -> Output: -5 (customer card costs more)

If I understood your question correctly, the following simple code should suffice:

def customer_card_discount1(total_purchase_amount1, discount_percent1, customer_card_price1):
    new_price = (total_purchase_amount1) * (100 - discount_percent1)/100 + (customer_card_price1)
    if new_price <= total_purchase_amount1:
        return ("You save {savings} euro, you should purchase a customer card,".format(savings=total_purchase_amount1 - new_price))
    else:
        return ("Don't buy a card, it would cost you {cost} euro more.".format(cost=total_purchase_amount1 - new_price))

A few comments:

  1. It may have been a typo at the time of writing your question on StackOverflow, but in your code, the total_purchase amount1 variable is missing an underscore, which would throw an error. It should be total_purchase_amount1.
  2. Inside the if-statements, you are returning the difference between customer_card_price1 and new_price, which's not really what you're looking for? You want to compare the total purchase price with the new price.
  3. Also, the next time you post on StackOverflow, try to include any errors that you're getting or to explain in more details what is it exactly that you're stuck on.
def profit(total_purchase,discount_percent,card_price):
    
    discount=discount_percent / 100 * total_purchase
    newpurchase= total_purchase - discount + card_price

    netamount=total_purchase-newpurchase
    # if netamount>0 then there would be benefit in buying card
    if netamount>0:
        print("Profit")
        return netamount

    # if netamount<=0 then there would be no benefit in buying card
    print("Loss")
    return netamount


print(profit(200,5,20))

Loss Output is -10.0

Related