#!/usr/bin/env python#--*--coding:utf-8--*--"""identifier legality check, first to start with a letter or underscore, followed by letters, underscores or numbers This example only check the length is greater than or equal to 2 of the indicator"""ImportStringalphas= String.letters +'_'Nums=string.digitsdefmenu ():Print 'Welcome to the Identifier Checker v1.0' Print 'Testees must is at least 2 chars long.' #myinput = raw_input (' Identifier to test?\n ')defRunidchk (myinput):ifLen (myinput) > 1:#whether the length of the interpretation is greater than 2 ifMYINPUT[0] not inchAlphas:#whether the first letter is a letter or an underscore Print "Invalid:first symbol must be alphabetic" Else: forOthercharinchMyinput[1:]: ifOtherchar not inchAlphas +Nums:Print "invalid:remaining symbols must be alphabetic" Break Else: Print "okay as an Identifier"if __name__=='__main__': whileTrue:menu () runidchk (Raw_input ('Identifier to test?\n'))
Running Result:
Kevins-macbook-pro:chapter06 kevin$ python idcheck.py Welcome to the Identifier Checker V1.0testees must is at least 2 cha RS Long. Identifier to Test?__hellookay as a identifierokay as an identifierokay as an identifierokay as a identifierokay as an I Dentifierokay as an identifierwelcome to the Identifier Checker V1.0testees must is at least 2 chars long. Identifier to Test?333invalid:first symbol must be alphabetic
Idcheck identifier Legality check