Use instances and differences in Python, such as exit, Return, Sys.exit ()

Source: Internet
Author: User
Tags exit in
There is this topic: string identifiers. Modify the idcheck.py script in example 6-1 so that it can detect an identifier of length one, and can identify the Python keyword, for the latter, you can use the keyword module (especially keyword.kelist) to help you.

My initial code is:

Copy the Code code 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 finished, I test each branch, and when I test to a branch, I have to enter an identifier such as _d4%, which contains illegal characters, 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), only the output return code is different):
Copy the Code code 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):
File "e:/python/idcheck.py", line Notoginseng, in
Sys.exit (0)
systemexit:0
>>>

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

Copy the Code code as follows:


Input your words,please!_d4%
_d4% isn ' t legal identifier for python!

Copy the Code code 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: If you replace Sys.exit () with exit in the following code, exit simply jumps out of the for loop that is closest to it, and print "%s is the legal identifier for PYTHON!2"% idinput statement is output, here, Exit acts like a break. But in fact, break and exit function differently

Copy the Code code 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
  • 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.