[英]Getting two largest numbers from three numbers without using conditional statement or loop in python 3.5
Complete the
function1()
function which is passed three whole numbers.The function returns the total of the two bigger numbers.完成传递三个整数的
function1()
函数,该函数返回两个较大数字的总和。
print()
print(1, function1(1, 2, 3))
print(2, function1(11, 12, 3))
print(3, function1(6, 2, 5))
output should be
1 5
2 23
3 11
This was the question. 这就是问题所在。 I try googling but all I have found was using conditional statement or loop.
我尝试使用谷歌搜索,但发现的全部是使用条件语句或循环。 I was wondering if there is other way to do this without using both?
我想知道是否还有其他方法可以同时使用两者? Reason being is that this question was on before the introduction of conditional statement and loop.
原因是这个问题是在引入条件语句和循环之前进行的。 I can advance my self-studying and do it however there must be a reason why it poped up before we go onto the next chapter.
我可以提高自己的学习水平,但是在我们进入下一章之前,一定有一个原因突然出现。 What do you reckon?.. (During self-studying for CS101) `
您怎么看?..(在学习CS101的过程中)`
you could sort the tuple of the numbers and add the first 2 values: 您可以对数字的元组进行排序并添加前两个值:
def function1(a, b, c):
s = sorted((a, b, c), reverse=True)
return s[0] + s[1]
sorted
starts with the smalles item normally that's why you need to set reverse=True
. sorted
通常从smalles项开始,这就是为什么需要设置reverse=True
。
or you could sum the three and subtract the minimal value: 或者您可以将这三个数相加并减去最小值:
def function1(a, b, c):
return sum((a, b, c)) - min(a, b, c)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.