Write a function that identifies whether a string conforms to a variable name in python syntax

2018-01-05 09:07:19
Number of readings: 115
#Write a function to identify whether the string conforms to the variable name of python syntax
 #import keyword import
 keyword
key_word = keyword.kwlist


def python_grammar(num): #Determine
     the input null character
 if num == "" :
        print( " Does not conform to python naming rules " )
         return
 count = 0
     while count < len(num):
         #Check that all characters in the string are numbers or letters or underscores "_"
 if (num[count].isalnum() = = True ) or (num[count] is "_" ):
             #Determine whether it is a keyword
 if num in key_word:
                print( " Does not conform to python naming rules " )
                 return
 #Determine whether the first character at the beginning is a number
 elif num[0].isdigit() == True :
                print( " Does not conform to python naming rules " )
                 return
         else :
            print( " Does not conform to python naming rules " )
             return
 #counter count
 += 1
     #After all the judgments are made, the rest are in conformity with
 print( " Conforms to python naming rules " )


print( "[ A variable name that identifies whether the string entered by the user conforms to python syntax ]" )
name = input( " Enter a string :" )
python_grammar(name)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325121813&siteId=291194637