Example of a loop exit in Python and a while loop

Source: Internet
Author: User
Tags exit in

Loop Exit

For loop:

For

Else

The For loop will execute the ELSE statement if it ends normally.


Script 1:

#!/usr/bin/env python

for I in Xrange (10):

Print I

Else:

Print "Main End"

Results:

[email protected] 20171227]# python exit.py

0

1

2

3

4

5

6

7

8

9

Main end

[Email protected] 20171227]#


Script 2:

#!/usr/bin/env python

Import time

For I in Xrange (10):

Print I

Time.sleep (1)

Else

Print "Main End"

Result: (Press CTRL + C interrupt)

[email protected] 20171227]# python exit.py

0

1

2

3

4

^ctraceback (most recent):

File "exit.py", line 6, <module>

Time.sleep (1)

Keyboardinterrupt

[Email protected] 20171227]#


Script 3:

Does not end normally:

#!/usr/bin/env python

Import time

For I in Xrange (10):

Print I

if i = = 5:

Break

Else

Print "Main End"

Results:

[email protected] 20171227]# python exit.py

0

1

2

3

4

5

[Email protected] 20171227]#


Script 4: (skip this cycle continue)

#!/usr/bin/env python

Import time

For I in Xrange (10):

if i = = 3:

Continue

if i = = 5:

Break

Print I

Else

Print "Main End"

Results:

[email protected] 20171227]# python exit.py

0

1

2

4

[Email protected] 20171227]#


Script 5: (Pass does nothing)

#!/usr/bin/env python

Import time

For I in Xrange (10):

if i = = 3:

Continue

elif i = = 5:

Break

Elif I ==6:

Pass #类似shell:

Print I

Else

Print "Main End"

Script 6:

#!/usr/bin/env python

Import time

Import Sys

For I in Xrange (10):

if i = = 3:

Continue

elif i = = 5:

Continue

Elif I ==6:

Pass

Elif I ==7:

sys.exit ()

Print I

Else

Print "Main End"

Print "hahaha"

Results:

[email protected] 20171227]# python exit.py

0

1

2

4

6

[Email protected] 20171227]#


Pip shows a third-party package

[[Email protected] 20171227]# PIP list

Deprecation:the default format would switch to columns in the future. You can use--format= (legacy|columns) (or define a format= (Legacy|columns) in your pip.conf under the [list] sections) to D Isable this warning.

Backports.ssl-match-hostname (3.4.0.2)

Chardet (2.2.1)

Configobj (4.7.2)

Decorator (3.4.0)

Iniparse (0.4)

IPy (0.75)

Ipython (1.2.1)

Jsonschema (2.5.1)

Kitchen (1.1.1)

Langtable (0.0.31)

Matplotlib (1.2.0)


Homework: Guess the number game

The system generates a random integer within 20.

Players have 6 chance to guess, every guess has feedback (guess big, guess small, guess right-end)

6 plays, guessed right, the player won.

Otherwise the system won .

In [1]: Import random

In [2]: Random.ran

Random.randint Random.random Random.randrange

In [2]: Random.randint (1,20)

OUT[2]: 14

In [3]: Random.randint (1,20)

OUT[3]: 6


Process Control-while Example

While and for relative ratio:

The For loop is used on a number of cycles.

The while loop is used for conditional control.

While loop:

The while loop is not exited until the expression becomes false. While loop, the expression is a logical expression and must return a true or false

Grammar:

While expression:

Statement (s)

Practice the script if:


Script 1:

#!/usr/bin/python

n = 0

While 1:

if n = = 10:

Break

Print N, ' Hellow '

N +=1

Results:

[email protected] 20171227]# python while.py

0 Hellow

1 Hellow

2 Hellow

3 Hellow

4 Hellow

5 Hellow

6 Hellow

7 Hellow

8 Hellow

9 Hellow

[Email protected] 20171227]#


Script 2:

#!/usr/bin/python

While 1:

print ' Hellow '

Input=raw_input ("Please input sth,q for exit.")

If input = = "Q":

Break

Results:

[email protected] 20171227]# python while.py

Hellow

Please input sth,q for EXIT.S

Hellow

Please input sth,q for exit.3

Hellow

Please input sth,q for EXIT.Q

[Email protected] 20171227]#


If the condition is false, it will also exit:

Script 3:

#!/usr/bin/python

Sth= "

While sth! = ' Q ':

print ' Hellow '

Sth=raw_input ("Please input sth,q for exit.")

Script 4:

Exit after Enter:

#!/usr/bin/python

Sth= "

While sth! = ' Q ':

print ' Hellow '

Sth=raw_input ("Please input sth,q for exit.")

If not sth:

Break

Script 5:

#!/usr/bin/python

Sth= "

While sth! = ' Q ':

print ' Hellow '

Sth=raw_input ("Please input sth,q for exit.")

If not STH:

Break

Else

print ' World '

Results:

[email protected] 20171227]# python while.py

Hellow

Please input sth,q for EXIT.Q

World

[Email protected] 20171227]#


Script 6:

#!/usr/bin/python

Sth= "

While sth! = ' Q ':

print ' Hellow '

Sth=raw_input ("Please input sth,q for exit.")

If not STH:

Break

if sth = = ' Quit ':

Continue

print ' Continue '

Else

print ' World '


Results:

[email protected] 20171227]# python while.py

Hellow

Please input sth,q for EXIT.SS

Continue

Hellow

Please input sth,q for EXIT.DF

Continue

Hellow

Please input sth,q for exit.quit

Hellow

Please input sth,q for EXIT.Q

Continue

World


Example of a loop exit in Python and a while loop

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.