如何简化此代码?

时间:2017-03-29 23:56:26

标签: python python-2.7

我想为这个函数输入一个参数(花费),然后让它返回一个字符串,说明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

2 个答案:

答案 0 :(得分:0)

您正在重复代码。此外,您可以返回值并立即显示,而不是在方法中打印结果。 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

答案 1 :(得分:0)

这样的事情会短得多。我没有测试过,但应该没问题。

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))