函数练习

import random def max_min_nums(x,y,*args): print(max(x,y,*args),min(x,y,*args)) max_min_nums(4,5,6),max_min_nums(*[random.randint(1,100) for i in range(10)])
无需解构,大大提升效率
def max_min_nums(x,y,*args): mx,mn = max(args),min(args) return min(x,y,mn),max(x,y,mx)
max_min_nums(2,3,1,4,1)
def max_min_nums(x,y,*args): mins,*_,maxs = sorted((x,y,*args)) return mins,maxs
def max_min_nums(x,y,*args): mx,mn = (y,x) if y > x else (x,y) for x in args: if x > mx: mx = x elif x < mn: mn = x return mx,mn
def get_max_min(): max_ = min_ = None while True: nums = input('please input nums').replace(',',' ').split() nums = [int(x) for x in nums] if not nums: break if max_ is None: max_ = min_ = nums[0] max_ = max(max_,*nums) min_ = min(min_,*nums) print(max_,min_)

浙公网安备 33010602011771号