Python, such as exit, return, and sys. exit ().
There is a question: String identifier. modify the idcheck of Example 6-1. py script to detect the identifier with a length of one and identify the Python keyword. For the latter requirement, you can use the keyword module (especially the keyword. kelist) to help you.
My initial code is:
Copy codeThe 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 => 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy'
# String. digits => '123'
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. During the test, we must enter _ d4 % and other identifiers that contain invalid characters to perform the test. I initially thought that sys. exit (0) --- exit the script normally, sys. exit (1) the script is unexpectedly exited, but the actual situation is/9sys. exit (1), only the output return code is different ):
Copy codeThe 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 37, in <module>
Sys. exit (0)
SystemExit: 0
>>>
As we can see, this does not achieve the following output, so where is the problem? Because sys. exit () always throws a SystemExit exception.
Copy codeThe Code is as follows:
Input your words, please! _ D4 %
_ D4 % isn' t legal identifier for Python!
Copy codeThe 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 => 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy'
# String. digits => '123'
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
Failed t SystemExit:
Pass
Except t:
Traceback. print_exc ()
The above code gets the SystemExit exception thrown by sys. exit.
Return: return the return value of a function from the function when defining the function. Terminate the function execution.
Exit: In the following code, if sys. exit () is replaced with exit, exit only jumps out of the for Loop closest to it. print "% s is legal identifier for Python! 2 "% idInput statement will be output. Here, exit is similar to break. But in fact, break and exit are different.
Copy codeThe 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