Use examples and differences in Python for exit, return, Sys.exit ()

Source: Internet
Author: User
Tags assert in python

This article mainly introduces Python exit, return, Sys.exit (), such as the use of examples and differences, this article is a practical project summary, the need for friends can refer to the

There is a problem with the string identifier. Modify Example 6-1 's idcheck.py script to detect length-one identifiers and identify the Python keyword, and you can use the keyword module (especially keyword.kelist) to help you with the latter requirement.

My initial code is:

The code is as follows:

#!/usr/bin/env python

Import string

Import keyword

Import Sys

#Get all keyword for python

#keyword. kwlist

#[' and ', ' as ', ' assert ', ' break ', ...]

KeyWords = Keyword.kwlist

#Get all character for identifier

#string. Letters ==> ' ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ '

#string. Digits ==> ' 0123456789 '

Charforid = String.letters + "_"

Numforid = String.digits

Idinput = raw_input ("Input your words,please!")

If Idinput in KeyWords:

print '%s is keyword fot python! '% idinput

Else

Lennum = Len (idinput)

if (1 = lennum):

if (Idinput in Charforid and Idinput!= "_"):

Print '%s is legal identifier for python! '% idinput

Else

#It ' s just "_"

print '%s isn ' t legal identifier for python! '% idinput

Else

if (Idinput[0:1] in Charforid):

legalstring = Charforid + Numforid

For item in IDINPUT[1:]:

If (item not in legalstring):

print '%s isn ' t legal identifier for python! '% idinput

Sys.exit (0)

Print '%s is legal identifier for python!2 '% idinput

Else

print '%s isn ' t legal identifier for PYTHON!3 '% idinput

After the code is complete, I test each branch, test to branch, must enter _d4% and so on contain illegal character identifiers to be able to test, I originally thought, sys.exit (0)---Normal exit script, Sys.exit (1) Abnormal exit script, but the actual situation is/ 9sys.exit (1), output return code is different only):

The code is as follows:

If (item not in legalstring):

print '%s isn ' t legal identifier for python! '% idinput

Sys.exit (0)

Input your words,please!_d4%

_d4% isn ' t legal identifier for python!

Traceback (most recent call last):

File "e:/python/idcheck.py", line Panax Notoginseng, in

Sys.exit (0)

systemexit:0

>>>

This shows that this does not achieve the output I expected the following effect, then, where is the problem? The Sys.exit () always throws a Systemexit exception.

The code is as follows:

Input your words,please!_d4%

_d4% isn ' t legal identifier for python!

The code is as follows:

#!/usr/bin/env python

Import string

Import keyword

Import Sys

Import Traceback

Try

#Get all keyword for python

#keyword. kwlist

#[' and ', ' as ', ' assert ', ' break ', ...]

KeyWords = Keyword.kwlist

#Get all character for identifier

#string. Letters ==> ' ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ '

#string. Digits ==> ' 0123456789 '

Charforid = String.letters + "_"

Numforid = String.digits

Idinput = raw_input ("Input your words,please!")

If Idinput in KeyWords:

print '%s is keyword fot python! '% idinput

Else

Lennum = Len (idinput)

if (1 = lennum):

if (Idinput in Charforid and Idinput!= "_"):

Print '%s is legal identifier for python! '% idinput

Else

#It ' s just "_"

print '%s isn ' t legal identifier for python! '% idinput

Else

if (Idinput[0:1] in Charforid):

legalstring = Charforid + Numforid

For item in IDINPUT[1:]:

If (item not in legalstring):

print '%s isn ' t legal identifier for python! '% idinput

Sys.exit ()

Print '%s is legal identifier for python!2 '% idinput

Else

print '%s isn ' t legal identifier for PYTHON!3 '% idinput

Except Systemexit:

Pass

Except

Traceback.print_exc ()

The above code gets the Systemexit exception thrown by Sys.exit ().

Return: Returns the return value of a function from a function when the function is defined, terminating the execution of the function.

Exit: In the following code, if you replace Sys.exit () with exit, exit only jumps out of its most recent for loop, and the print "%s is legal identifier for PYTHON!2"% idinput statement is output, here, Exit acts like a break. But actually the break and exit functions are different

The code is as follows:

For item in IDINPUT[1:]:

If (item not in legalstring):

print '%s isn ' t legal identifier for python! '% idinput

Sys.exit ()

Print '%s is legal identifier for python!2 '% idinput

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.