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