我需要用Python编写一个使用def的程序来计算以折扣价购买客户卡是否有利可图(这意味着折扣+卡价格后的购买成本低于或等于原始成本),其中有以下论点:以欧元表示的总购买金额、以客户卡计算的折扣百分比和客户卡价格。如果卡为客户节省了一些x欧元,则函数应该返回x。如果购买+卡的价格比最初的购买价格更贵,则返回-x,其中x是欧元的数量,这张卡更便宜。输出应该是这样的:总购买量: 200 %的折扣:5客户卡价格:5最好买张卡,到目前为止我已经写好了这段代码,但是我不知道下一步该做什么:
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"))发布于 2022-09-22 11:04:46
你可能想得太多了。如果我正确理解了它,你只需要比较原始价格(没有任何折扣)和新价格(原价,加上卡价减去客户折扣)。这导致了一个简单得多的解决方案:
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))示例
5
200 / 5 / 5 ->输出:200 / 5 / 5 200 / 5 / 15 ->输出:-5 (客户卡成本更高)发布于 2022-09-22 11:11:43
如果我正确理解了您的问题,下面的简单代码就足够了:
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))几点意见:
total_purchase amount1变量缺少下划线,这会引发错误。应该是total_purchase_amount1.customer_card_price1和new_price之间的区别,这不是您想要的吗?您希望将总采购价格与新价格进行比较。发布于 2022-09-22 11:23:15
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))损耗输出为-10.0
https://stackoverflow.com/questions/73813342
复制相似问题