首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在python中使用代码以提高盈利能力

问在python中使用代码以提高盈利能力
EN

Stack Overflow用户
提问于 2022-09-22 10:50:32
回答 3查看 100关注 0票数 0

我需要用Python编写一个使用def的程序来计算以折扣价购买客户卡是否有利可图(这意味着折扣+卡价格后的购买成本低于或等于原始成本),其中有以下论点:以欧元表示的总购买金额、以客户卡计算的折扣百分比和客户卡价格。如果卡为客户节省了一些x欧元,则函数应该返回x。如果购买+卡的价格比最初的购买价格更贵,则返回-x,其中x是欧元的数量,这张卡更便宜。输出应该是这样的:总购买量: 200 %的折扣:5客户卡价格:5最好买张卡,到目前为止我已经写好了这段代码,但是我不知道下一步该做什么:

代码语言:javascript
复制
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"))
EN

回答 3

Stack Overflow用户

发布于 2022-09-22 11:04:46

你可能想得太多了。如果我正确理解了它,你只需要比较原始价格(没有任何折扣)和新价格(原价,加上卡价减去客户折扣)。这导致了一个简单得多的解决方案:

代码语言:javascript
复制
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

  • Input:

  • 输入:200 / 5 / 5 ->输出:200 / 5 / 5 200 / 5 / 15 ->输出:-5 (客户卡成本更高)
票数 1
EN

Stack Overflow用户

发布于 2022-09-22 11:11:43

如果我正确理解了您的问题,下面的简单代码就足够了:

代码语言:javascript
复制
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))

几点意见:

  1. 在编写StackOverflow问题时可能是一个错误,但在代码中,total_purchase amount1变量缺少下划线,这会引发错误。应该是total_purchase_amount1.
  2. Inside -if语句,您正在返回customer_card_price1和new_price之间的区别,这不是您想要的吗?您希望将总采购价格与新价格进行比较。
  3. 也是这样,下次您在StackOverflow上发帖时,请尽量包括您正在收到的任何错误,或者详细说明您所坚持的到底是什么。
票数 0
EN

Stack Overflow用户

发布于 2022-09-22 11:23:15

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73813342

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档