使用Python條件判斷實現簡單的電話號碼歸屬地判斷

判斷號碼的歸屬地

#中國移動
ChinaMobile = ['134','135','136','137','138','139','147','150','151','152','157','158','159','178','182','183','184','187','188','198']
#中國聯通
ChinaUnicorn = ['130','131','132','145','155','156','166','171','175','176','185','186']
#中國電信
ChinaTelecom = ['133','149','153','173','177','180','181','189','199']
#電話號碼的長度
tellen = 11
#這裏使用一個死循環,可以一直查詢
while 1:
    telnum = input('請輸入你要查詢的電話號碼:')
    if telnum.isdigit() == False:#判斷裏面輸入的電話號碼是否有非法字符
        print('輸入的電話號碼包含非法字符哦,請重新輸入')
        continue
    elif len(telnum) < tellen:
        print('輸入的電話號碼位數太少了哦,請重新輸入')
        continue
    elif len(telnum) > tellen:
        print('輸入的電話號碼位數太多了哦,請重新輸入')
        continue
    elif len(telnum) == tellen:
    #取輸入號碼的前三位進行判斷歸屬地
        if telnum[:3] in ChinaMobile:
            print('你輸入的電話號碼是中國移動號碼哦')
        elif telnum[:3] in ChinaUnicorn:
            print('你輸入的電話號碼是中國聯通號碼哦')
        elif telnum[:3] in ChinaTelecom:
            print('你輸入的電話號碼是中國電信號碼哦')
        else:
            print('你輸入的可能不是電話號碼哦,請檢查')

裏面用到的一個函數:isdigit()
這個函數直接調用即可,telnum.isdigit()
1、當telnum裏面的內容全爲數字時,函數返回True
2、當elnum裏面的內容不全爲數字時,函數返回False

#############堅持的第三天,繼續下去

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章