python variable number of parameters (multi-parameter returns the number of arguments required, maximum, maximum)

 

 A custom sequence of digits required number of parameters, the maximum value, the maximum value () --------- Method a:

def max(*a):
	m=a[0]
	p=a[0]
	n=0
	for x in a:
		if x>m:
			m=x
		n+=1
	for x in a:
		if x<p:
			p=x
	return n,m,p
if __name__ == '__main__':
   list=max(3,4,5)
      print("参数个数{},最大值{},最小值{}".format(list[0],list[1],list[2]))

  Used herein is a plurality of return value, wherein when it is desired only when a value, the return value may be an array acceptable, and separated by the subscript.

Two, input the number of parameters required input string of numbers, a maximum value, a maximum value () --------- Method two:

  

def max(*a):
	m=a[0]
	p=a[0]
	n=0
	for x in a:
		if x>m:
			m=x
		n+=1
	for x in a:
		if x<p:
			p=x
	return n,m,p

if __name__ == '__main__':
		b=eval(input())
		list=max(*b)
		print("参数个数{},最大值{},最小值{}".format(list[0],list[1],list[2]))

  Also here is the input string of numbers, with eval (input ()) are separated by a comma default b =, and converted to digital, and these series of numbers as a variable number of parameters as a function of operation parameters

     b = eval (input ()) can be used instead of these two lines

 

    x = input ( "Enter a number of the number, separated by commas") .split ( ",")

    b=[int(i) for i in x]

    

Guess you like

Origin www.cnblogs.com/cybg/p/11646838.html