Python 我有一个特定的编程任务
给定3个整数值,a b c返回它们的和。但是,如果其中任何一个值是青少年(包括13..19),则该值将计为0,但15和16不计为青少年。编写一个单独的助手“def fix_teen(n):”接受一个int值,并返回为teen规则确定的值。通过这种方式,您可以避免重复青少年代码3次(即“分解”)。在下面定义辅助对象,其缩进级别与主no_teen_sum()相同 我的解决方案:-Python 我有一个特定的编程任务,python,list,sum,Python,List,Sum,给定3个整数值,a b c返回它们的和。但是,如果其中任何一个值是青少年(包括13..19),则该值将计为0,但15和16不计为青少年。编写一个单独的助手“def fix_teen(n):”接受一个int值,并返回为teen规则确定的值。通过这种方式,您可以避免重复青少年代码3次(即“分解”)。在下面定义辅助对象,其缩进级别与主no_teen_sum()相同 我的解决方案:- def no_teen_sum(a,b,c): sum=0 lst=[13,14,15,16,17,18
def no_teen_sum(a,b,c):
sum=0
lst=[13,14,15,16,17,18,19]
def fix_teen(n):
s=0
if n >=13 and n<= 19:
if n==15:
s=15
elif n==16:
s=16
else:
s=0
return s
if (a not in lst) and (b not in lst) and (c not in lst):
sum=a+b+c
elif a in lst :
sum=fix_teen(a)+b+c
if b in lst:
sum=fix_teen(a)+fix_teen(b)+c
elif b in lst:
sum=a+fix_teen(b)+c
if c in lst:
sum=a+fix_teen(b)+fix_teen(c)
elif c in lst:
sum=a+b+fix_teen(c)
if a in lst:
sum=fix_teen(a)+b+fix_teen(c)
else:
sum=fix_teen(a)+fix_teen(b)+fix_teen(c)
return sum
如有任何建议,我们将不胜感激。提前感谢……问题在于你是如何写的。您只需将号码发送至fix teen,并按如下方式书写:
def fix_teen(n):
if (n >= 13 and n <=19):
if (n!= 15 and n != 16):
n = 0
return n
这将缩短您的代码并修复错误。您使事情变得超级复杂:您应该不要编写所有那些
if
语句,只要简单地概括一下修复(n)
规则就足够了
如果一个数字在13到19之间,而不是15或16之间,那么这个数字就是青少年。所以我们可以写:
def fix_teen(n):
if n >= 13 and n <= 19 and (n < 15 or n > 16):
return 0
return n
我们也可以使用*args
轻松概括这一点:
def no_teen_sum(*args):
return sum(fix_teen(n) for n in args)
现在我们可以用任意数量的值来调用它。这导致:
>>> no_teen_sum(14,1,13)
1
>>> no_teen_sum(14,2,17)
2
>>> no_teen_sum(16,17,18)
16
>>> no_teen_sum(17,18,19)
0
更简单的列表理解方法:
def no_teen_sum(*args): # *args means that it can take any number of arguments and pass it as a tuple to the function
no_teens = [n for n in args if any([n in (15, 16), n not in range(13, 20)])] # List comprehension that will iterate the passed arguments and omit anything between 13-19 except 15 & 16
return sum(no_teens) # return the sum
编辑:在再次阅读您的问题后,我注意到您需要一个teen_fix
方法,我假设这是作业的一个要求,在这种情况下,使用以下解决方案,尽管上述解决方案完全有效
def no_teen_sum(a, b, c):
no_teens = [teen_fix(n) for n in [a, b, c]]
return sum(no_teens)
def teen_fix(n):
return n if any([n in (15, 16), n not in range(13, 20)]) else 0
当
fix\u teen
应该处理所有这些时,你为什么要写所有lst
的东西呢?而且,fix\u teen
应该在no\u teen\u sum
之外。当a
首先被计算,但是a
和c
是“青少年”时会发生什么?我认为如果lst:sum=fix_teen(a)+b+fix_teen(c)中的a将永远不会被执行。@user2357112将fix_teen
声明为中的内部函数no_teen_sum
从封装的角度来看是有意义的,如果代码中没有其他任何东西使用它。@christopherkeyelhorton:Eh,它在模块级封装得非常有用,将其放在模块级会使它更易于测试。在范围(a,b)
中进行范围测试是一种不好的方法,因为在Python 2上或对于非int输入,它会降低到O(b-a)
性能,在许多情况下,如果您希望True
或出现异常,它会以静默方式返回False
。@user2357112:否,在Python-3中,它是在O(1)中完成的。例如,100范围内(1000000000000)
。这是以纳秒为单位处理的,我说过“在Python2上或对于非int输入”。对于Python3和ints,它是O(1),但是如果您在Python2上尝试它,或者如果您无意中使用3.0
或“3”
,它所需的时间与范围的长度成比例。@user2357112:是的,我同意,因为它构建了一个当然列表。不管怎样,我展示了一种比较的方法。它不应该是all(…)
?@WillemVanOnsem:no,因为如果n=15
,那么n在(15,16)
中计算为True
而n不在范围(13,20)
计算为False
,在这种情况下all()
将计算为False
,是 啊我没注意到你用了否定的逻辑。
def no_teen_sum(*args):
return sum(fix_teen(n) for n in args)
>>> no_teen_sum(14,1,13)
1
>>> no_teen_sum(14,2,17)
2
>>> no_teen_sum(16,17,18)
16
>>> no_teen_sum(17,18,19)
0
def no_teen_sum(*args): # *args means that it can take any number of arguments and pass it as a tuple to the function
no_teens = [n for n in args if any([n in (15, 16), n not in range(13, 20)])] # List comprehension that will iterate the passed arguments and omit anything between 13-19 except 15 & 16
return sum(no_teens) # return the sum
def no_teen_sum(a, b, c):
no_teens = [teen_fix(n) for n in [a, b, c]]
return sum(no_teens)
def teen_fix(n):
return n if any([n in (15, 16), n not in range(13, 20)]) else 0