簡體   English   中英

如何簡化此代碼?

[英]How can I simplify this code?

我想向此函數輸入一個參數(支出),然后讓它返回一個字符串,說明9個可能性(plan500_90,plan500_80等)中哪一個是最低的。

它目前正在工作,但似乎比必須的時間更長。

def woof(spend):
    plan500_90 = 35.05 * 12
    plan500_80 = 32.63 * 12
    plan500_70 = 29.85 * 12
    plan250_90 = 42.66 * 12
    plan250_80 = 39.41 * 12
    plan250_70 = 35.80 * 12
    plan100_90 = 53.38 * 12
    plan100_80 = 49.10 * 12
    plan100_70 = 44.41 * 12

    if spend > 500:
        a = plan500_90 + spend - 0.90 * (spend - 500)
        b = plan500_80 + spend - 0.80 * (spend - 500)
        c = plan500_70 + spend - 0.70 * (spend - 500)
    else:
        a = plan500_90 + spend
        b = plan500_80 + spend
        c = plan500_70 + spend
    if spend > 250:
        d = plan250_90 + spend - 0.90 * (spend - 250)
        e = plan250_80 + spend - 0.80 * (spend - 250)
        f = plan250_70 + spend - 0.70 * (spend - 250)
    else:
        d = plan250_90 + spend
        e = plan250_80 + spend
        f = plan250_70 + spend
    if spend > 100:
        g = plan100_90 + spend - 0.90 * (spend - 100)
        h = plan100_80 + spend - 0.80 * (spend - 100)
        i = plan100_70 + spend - 0.70 * (spend - 100)
    else:
        g = plan100_90 + spend
        h = plan100_80 + spend
        i = plan100_70 + spend

    list1 = [a, b, c, d, e, f, g, h, i,]

    if min(list1) == a:
        print "Plan500_90 is the cheapest plan at $%d per year." % a
    elif min(list1) == b:
        print "Plan500_80 is the cheapest plan at $%d per year." % b
    elif min(list1) == c:
        print "Plan500_70 is the cheapest plan at $%d per year." % c
    elif min(list1) == d:
        print "Plan250_90 is the cheapest plan at $%d per year." % d
    elif min(list1) == e:
        print "Plan250_80 is the cheapest plan at $%d per year." % e
    elif min(list1) == f:
        print "Plan250_70 is the cheapest plan at $%d per year." % f
    elif min(list1) == g:
        print "Plan100_90 is the cheapest plan at $%d per year." % g
    elif min(list1) == h:
        print "Plan100_80 is the cheapest plan at $%d per year." % h
    elif min(list1) == i:
        print "Plan100_70 is the cheapest plan at $%d per year." % i

您正在重復代碼。 同樣,您可以返回值並立即顯示,而不是在方法中打印結果。 另外,如果不需要,則使用if-else,因為min()函數無論如何都返回最小值。

def woof(spend):
    plan500_90 = 35.05 * 12
    plan500_80 = 32.63 * 12
    plan500_70 = 29.85 * 12
    plan250_90 = 42.66 * 12
    plan250_80 = 39.41 * 12
    plan250_70 = 35.80 * 12
    plan100_90 = 53.38 * 12
    plan100_80 = 49.10 * 12
    plan100_70 = 44.41 * 12


    a = plan500_90 + spend
    b = plan500_80 + spend
    c = plan500_70 + spend

    d = plan250_90 + spend
    e = plan250_80 + spend
    f = plan250_70 + spend

    g = plan100_90 + spend
    h = plan100_80 + spend
    i = plan100_70 + spend

    if spend > 500:
        a = a - 0.90 * (spend - 500)
        b = b - 0.80 * (spend - 500)
        c = c - 0.70 * (spend - 500)

    if spend > 250:
        d = d - 0.90 * (spend - 250)
        e = e - 0.80 * (spend - 250)
        f = f - 0.70 * (spend - 250)

    if spend > 100:
        g = g - 0.90 * (spend - 100)
        h = h - 0.80 * (spend - 100)
        i = i - 0.70 * (spend - 100)

    list1 = [a, b, c, d, e, f, g, h, i,]

    return min(list1)

# Any spending variable
spend = 1000

cheapest_plan = woof(spend)
print "Plan100_70 is the cheapest plan at $%d per year." % cheapest_plan

這樣的事情會短很多。 我沒有測試過,但應該沒問題。

class Plan:
    def __init__(self, monthly_spend, min_spend, discount):
        self.yearly_spend = monthly_spend * 12
        self.min_spend = min_spend
        self.discount = discount

    def cost(self, spend):
        return float(self.yearly_spend + spend - self.discount * max(spend - self.min_spend, 0))

    def __str__(self):
        return 'Plan{}_{}'.format(self.min_spend, self.discount)

plans = [Plan(35.05, 500, .9), Plan(32.63, 500, .8)]  # TODO: fill in rest

def get_cheapest_plan(spend):
    cheapest_plan = min(plans, key=lambda x: x.cost(spend))
    print('{} is the cheapest plan at ${:.02} per year'.format(str(cheapest_plan), cheapest_plan.cost(spend))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM