[英]Tuple function that returns certain parameters
I'm stuck on an exercise where I should do a function which makes a tuple out of 3 given numbers and returns tuple following these rules:我被困在一个练习中,我应该做一个 function,它从 3 个给定的数字中生成一个元组,并按照以下规则返回元组:
For example:例如:
> print(do_tuple(5, 3, -1))
# (-1, 5, 7)
What I have so far:到目前为止我所拥有的:
def do_tuple(x: int, y: int, z: int):
tuple_ = (x,y,z)
summ = x + y + z
mini = min(tuple_)
maxi = max(tuple_)
if __name__ == "__main__":
print(do_tuple(5, 3, -1))
I know I should be able to sort and return these values according to the criteria but I can't work my head around it..我知道我应该能够根据标准对这些值进行排序和返回,但我无法解决这个问题……
You need to return the tuple inside your function您需要返回 function 中的元组
def do_tuple(x: int, y: int, z: int):
tuple_ = (x,y,z)
summ = x + y + z
mini = min(tuple_)
maxi = max(tuple_)
return (mini, maxi, summ)
if __name__ == "__main__":
print(do_tuple(5, 3, -1))
As already indicated in a previous answer, you just have to add a return
statement in your function. Additionnally, you can use packing to simplify and manage a variable number of arguments. Finally it results in a one-lined and easy-to-read function, as follows:正如前面的答案中已经指出的那样,您只需在 function 中添加一个
return
语句。此外,您可以使用打包来简化和管理可变数字 arguments。最后它会产生一行且易于阅读function,如下:
def do_tuple(*args):
return (max(args), min(args), sum(args))
print(do_tuple(5, 3, -1)) # (5, -1, 7)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.