Seeking a number of maximum and minimum number of

method one:

Because they can not determine the number of inputs, first determine the number of inputs, number of inputs of a few, a few cycles later will, if there is only one number, the maximum and minimum values ​​are equal, when the number is more than one, they are the maximum value and minimum value comparison, if there is assigned a maximum value greater than the maximum, if less than the minimum, then it is assigned to the minimum.

. 1 COUNT = int (INPUT ( ' number of input data: \ n- ' ))  
 2 A =. 1
 . 3  the while A <= COUNT:
 . 4     m = int (INPUT ( ' Please enter {number}: ' .format (A ))) # method string 
. 5      IF a ==. 1:               # sentence will be executed, and is performed only once, the first aim is to make as according to the number you entered is compared with the number after 
. 6          max = min = NUM      # several second and later will go else, 
7      else :                    # the first time you go else, comparisons min and max is the first time you enter a number, after walking else might not be 
8          IF NUM < min:
 . 9              min =NUM
 10          elif NUM> max:
 . 11              max = NUM
 12 is      A +. 1 =
 13 is  Print ( ' maximum data: ' , max)
 14  Print ( ' the minimum data is: ' , min)

Method Two:

1  # ideas: for each data user input into a list, and then calls min and max functions as reference in the list can be a 
2 COUNT = int (INPUT ( ' Enter the number of data: \ n- ' ))
 . 3 = List []
 . 4  for I in range (. 1,. 1 + COUNT):   # Note range range 
. 5    list.append (int (iNPUT ( ' enter a value of {}: ' .format (I))))
 . 6   
. 7  Print (min (List))
 . 8  Print (max (List))

The second: Array evaluation known know the maximum and minimum

. 1  DEF max (* A): # -defined function, max is the function name, the internal brackets parameter function of 
2      m = A [0]
 . 3      for X in A:
 . 4          IF X> m:
 . 5              m = X # m is the maximum value 
. 6      return m 
 . 7  DEF min (* a): # -defined function, min is the function name, the internal brackets function parameters 
. 8      m = a [0]
 . 9      for X in a:
 10          IF X < m:
 . 11              m = X # m is the minimum value 
12 is      return m
13 is  DEF NUM (* A): # -defined function, the function name is NUM, the internal parameter function of brackets 
14      m = len (A) # m is the number of parameters, Python len () method returns the object (character, list, meta ) length or the number of items and the like groups. 
15      return m
 16  
. 17  IF  the __name__ == ' __main__ ' :
 18 is      X, Y, Z = 1,10,20 
 . 19      Print ( " max: " , max (X, Y, Z) ) 
 20 is      Print ( " min: " , min (X, Y, Z)) 
 21 is      Print ( " NUM: " , NUM (X, Y, Z))

 

Guess you like

Origin www.cnblogs.com/qq991025/p/11653480.html