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.
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.
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))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.