Python核心編程第2版第六章習題答案

import string
import keyword
import sys

Startwith=string.ascii_letters+'_'
Othersymbol=string.digits


def CheckID(s):
    if s[0] in Startwith:
        if len(s)==1:
            print("The ID is valid")
            if s in keyword.kwlist:
                print ("And the ID is a key word")
        else:
            for j in s[1:]:
                if j not in Othersymbol+Startwith:
                    print ("The other symbol is invalid.")
#                    sys.exit()
                    break
            print ("The ID is valid.")
            if s in keyword.kwlist:
                print ("And the ID is a key word")
    else:
        print ("The startwith is invalid.")


if __name__=="__main__":
    sid=input("Enter a string:")
    CheckID(sid)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章