[英]How can I simplify this code?
I want to input a single argument (spend) to this function, and then have it return a string stating which of the 9 possibilities (plan500_90, plan500_80, etc.) is the lowest number. 我想向此函数输入一个参数(支出),然后让它返回一个字符串,说明9个可能性(plan500_90,plan500_80等)中哪一个是最低的。
It's currently working but seems much longer than it has to be. 它目前正在工作,但似乎比必须的时间更长。
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
You are repeating the code. 您正在重复代码。 Also instead of printing the result in the method, you can return the value and show at once. 同样,您可以返回值并立即显示,而不是在方法中打印结果。 Also the if-else if not needed, as min() function returns minimum anyways. 另外,如果不需要,则使用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
something like this will be a lot shorter. 这样的事情会短很多。 i haven't tested it, but should be ok. 我没有测试过,但应该没问题。
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.