python教程6-2:字符串标识符

时间:2023-03-09 02:23:11
python教程6-2:字符串标识符

标识符合法性检查。

1、字母或者下划线开始。
2、后面是字母、下划线或者数字。
3、检查长度大于等于1。
4、可以识别关键字。

python35 idcheck.py 

idcheck.py

import string
import keyword

alphas = string.ascii_letters + '_'
nums = string.digits
keywords = keyword.kwlist

print ("Welcome to the Identifier Checker v1.1")
print ("Testees must be at least 1 char long.")
myInput = input('Identifier to test?')

if myInput not in keywords:
    if len(myInput) >=
1:
     
  if myInput[0] not in alphas:
     
      print
("Waring:first symbol must be alphas.")
     
  
     
  else:
     
      for
otherChar in myInput[1:]:
     
     
    if otherChar not in alphas +
nums:
     
     
     
  print ("Waring:other char must be
alphas||nums||_")
     
     
     
  break
     
     
else:
     
     
    print ("The " + myInput +" is
a identifier.")
    else:
     
  print ("The len fault.least 1.")
else:
    print ("The " + myInput
+ " is a keyword.")