python base (4): user interaction, if determined, while loop, break and continue

1. The user interaction

Using the input () function allows us to interact with computers up

grammar:

Content = input (Tips Information)

Individual cases from the content can be sent directly to the user input of Use

the INPUT = Content ( " you eat yet? " )
 Print ( " we received in the console: " + Content)

2. Process control if statement

You live in ⽣ is not often encounter a variety of options, such as playing discolored ⽐ submenus, guess zoomed lower, and select ⾛ ⽐ as the road to go home? Python program will encounter this situation, which Use the if statement.

First Time kinds of syntax:

if conditions are: # quotes is to separate conditions and results.

  Results 1. # Four spaces, or a tab key ⼀, this is to tell the full program of this condition EMPTY

Results.

If the condition is true (True) Perform a result, then the result of 2, if the conditions are false (False) as a direct result 2

88 = Money IF Money> 500:     Print ( "taxi home") Print ( "I'm home")

 

Second shot kinds of syntax:

If conditions:

  Results 1

else:

  Results

Code 3

the INPUT = Gender ( " Excuse me, are you a man or a woman? " )
 IF Gender == ' M ' :
      Print ( " your gender is male " )
 the else :    # is not a man 
     Print ( " your gender is female ." )

The third syntax:

If Condition 1:

  Results 1

elif Condition 2:

  Results

..

else:

  N results

the INPUT = month The ( " Please enter a month: " )
 IF month The == ' Jan ' :
     Print ( " dumplings " )
 elif month The == ' February ' :
     Print ( " Apple " )
 elif month The == ' March ' :
     Print ( " sweet-scented osmanthus cake " )
 elif month The == ' March ' :
     Print ( " watermelon ")
elif month The == ' March ' :
     Print ( " Dumplings " )
 elif month The == ' March ' :
     Print ( " popsicles " )

The fourth syntax (nesting):

If Condition 1:

  Results 1

  If Condition 2:

    Results

  else:

    Results

else:

  Results 4

⽆ may limit nesting, but try not to exceed three nested in the actual development

Print ( " ? bang bang bang, Who " ) 
Gender = the INPUT ( " Excuse me, are you a man or a woman? " )
 IF Gender == ' M ' :
    # Pass Pass through # syntax for integrity. 
    Print ( " .. men go next door to find alex " )
 the else :    # is not a man 
    Age = the INPUT ( " How old up? " )
     IF int (Age)> 48: # the INPUT string 48 is received both int. type of data is not comparable 
       Print ( " Ma'am, are you looking for? may be next door " )
     the else:
        Print ( " my house melons, particularly sweet. " )

3. The flow control while loop

In ⽣ live, we come across things cycle, right? The cycle ⽐ songs. In the program, but also keep it, and this is an empty statement processes while.

Cyclic analog ⼀ songs:

the while True: 

  Print ( ' we do not like ⼀ ' ) 

  Print ( ' We all kind ⼀ ' ) 

  Print ( ' so what's not to like ⼀ ' )

 

while conditions:

  result

If the condition is true, Perform direct result of then determine the condition again until the condition is false. Stop the cycle

Then how do we end for still circulating it?

End of the cycle:

1. Change the conditions.

2.break

4. The flow control break and continue

1.break: once out of the loop. Interrupt means

2.continue: Stop this cycle, continue to Perform next time the cycle.

Guess you like

Origin www.cnblogs.com/liuhui0308/p/11799590.html