python- control structure

First, the basic structure of the program

Program flow chart

 

 

Second, a simple branch

 1、if<condition>:<body>  

      else:<body>  

2, relational operators: <, <=, ==,> =,>, =!

3 Examples

## Solving quadratic 
Import Math 
DEF main (): 
    A, B, C = the eval (INPUT ( 'Enter equation three parameters (A, B, C):')) 
    Delta. 4-B * = B * C * a 
    IF a == 0: 
        X = -b / C 
        Print ( '\ n-not a quadratic equation of the equation, only one solution is:', X) 
    elif Delta <0: 
        Print ( 'without the quadratic equation real root! ') 
    elif Delta == 0: 
        discRoot Math.sqrt = (Delta) ## square root 
        the root1 = (-b + discRoot) / (2 * a) 
        Print (' \ n-root of the equation only as a weight: ', the root1) 
    the else: 
        discRoot Math.sqrt = (Delta) ## square root 
        the root1 = (-b + discRoot) / (2 * A) 
        root2 = (-b - discRoot) / (2 * A) 
        Print (' \ n two real roots of the equation are: ', the root1, root2) 
main ()

 

 

Third, the exception handling

1、try.......except......

 

 

True the while: 
    the try: 
        the X-= int (the INPUT ( 'Please enter a number:')) 
        BREAK 
    the except Va lueError: 
        Print ( '! not a valid digital input enter again!')

 

2、try......except........else......finally.....

there is an abnormality try to perform except, without exception executed else, regardless of whether an exception occurs finally have to perform

 

main DEF (): 
    the try: 
        num1, num2 = the eval (INPUT ( 'Enter two digits do division, Comma Separated:')) 
        Result = num1 / num2 
    the except the ZeroDivisionError: 
        Print ( '0 divisor can not do!') 
    SyntaxError the except: 
        Print ( '! least enter a comma') 
    the except: 
        Print ( 'input error!') 
    the else: 
        Print ( 'no error, the calculation result is:', result) 
    the finally: 
        Print ( 'last statement executed .. . ') 
main ()

  

## Solving quadratic 
Import Math 
DEF main (): 
    : the try 
        ( 'Enter. 3 equation parameters (A, B, C):' INPUT ()) A, B, C = the eval 
        Delta B * = B- A * C *. 4 
        discRoot Math.sqrt = (Delta) ## square root 
        the root1 = (-b + discRoot) / (2 * A) 
        root2 = (-b - discRoot) / (2 * A) 
        Print ( '\ n two real roots of the equation are: ', the root1, root2) 
    the except a ValueError aS excObj: 
        IF STR (excObj) ==' Math Domain error ': 
            ! Print (' no real roots ') 
        the else: 
            Print (' no to correct the coefficient ') 
    the except NameError: 
        ! Print (' not three digital inputs') 
    the except TypeError: 
        Print ( 'the digital input is not') 
    the except SyntaxError:
        print ( 'input in the form of error, may not enter a comma-separated'))
    except:
        print ( 'Other types of errors') 
main ()

 

Examples ---- find out the three largest

The maximum number of ## N 
: DEF main () 
    ( 'the number of input values:' INPUT ()) the eval = n- 
    max = the eval (INPUT ( 'input first number >>>>')) 
    for I Range in (n--. 1): 
        X = the eval (iNPUT ( 'enter the next number >>>>')) 
        IF X> = max: 
            max = X 
    Print ( 'maximum value is:', max) 
main () 

# # max () function of 
x, y, z = eval ( input ( ' enter number 3:')) 
Print ( 'maximum value is:', max (x, y , z))

  

Fourth, the loop structure

 1, for loop

         The need to provide a fixed number of cycles

Each element in the list loop ## 
words = [ 'Hello', 'Sleeping', 'tommorrow'] 
for words in W: 
    Print (W, len (W))

  2, while circulation

continue --- terminate this cycle

break ----- terminate the entire cycle

 Loop statement else

in n-Range for (2,10): 
    for X in Range (2, n-): 
        IF% n-X == 0: 
            Print (n-, '=', X, '*', X n-//) 
            BREAK 
    the else: 
        print (n, 'is a prime number')

  

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/motoharu/p/11569214.html