Exception handling in python library of random and procedures

1.random library use:

random library using the Python standard library of random numbers
from probability theory point of view, the random number data (such as flipping a coin) randomly generated, but the computer is not possible to generate random values, true random number is generated under certain conditions determine the value, but we do not understand these conditions, or beyond our ken. The computer can not generate truly random numbers, it will be referred to as pseudo-random number nonce
- a pseudo random number: computer by using (pseudo) random sequence elements Mersenne Twister generation
function of generating a pseudo-random number for the python library is random
because it is the standard library, when you need to use only import random

random library of commonly used functions:

 

 random library of reference methods and math libraries, there are two ways:

import random or

from random import *

Some examples of usage of the random library as follows:

>>> from Random Import * 
>>> Random ()
 0.4283719374862134 

>>> Uniform (1,10 )
 3.1486184624816849 

>>> randrange (0,100,4)        # from 0 to 100 in increments of a random element to return in 4 
56 

 >> > Is = List (Range (10 ))
 >>> shuffle (LS)
 >>> Print (lS) 
[ 4,1,7,3,0,9,5,2,8,6 ]

     

2. The exception handler:

Exception handling is common statement: try-except

The basic syntax is:

try:

  <A statement block>

except:

  <Statement block two>

You can look at the following example:

NUM = the eval >>> (INPUT ( " Please enter an integer: " ))
 >>> Print (NUM ** 2 )

 >>> 
enter an integer: 100 
10000 
>>> 
enter an integer: NO 
Traceback (MOST Last Call Recent): 
File " D: PythonPL the I / echoInt.py " , Line. 1, in <Module1> 
NUM = the eval (iNPUT ( " Please enter an integer: " )) File " <String> " , Line. 1, in <Module1> NameError: name   ' No ' is not defined

Can be seen when the digital input, the normal operation of the program, not when the digital input, python interpreter returns exception information, and exit the program

the try : 
    NUM = the eval (INPUT ( " Enter - an integer: " )
     Print (NUM ** 2 )
 the except NameError:
     Print ( " ! input error, enter an integer " )

 >>> 
enter an integer: NO 
Input error, enter an integer!

In addition, try-except statement can support multiple statements except

the try : 
    ALP = " ABCDEFGHI JKLMNOPQRSTUVWXYZ" 
    IDX = eval (the INPUT ( " Jing Enter an integer: " ))
     Print ALP [IDX])
 the except NameError:
     Print ( " Enter the wrong Minamata, clear input - - integer! " )
 the except :
     Print ( " other wrong-ho " )


 >>> 
enter an integer: NO 
input error, enter an integer!
 >>> 
enter an integer: 100 other error

 

          

 

Guess you like

Origin www.cnblogs.com/jackyfive/p/11745212.html