Python 基础

Name:  标识符检查 ID CHECK

Purpose:  用来检查python有效标识符的小脚本。python的标识符必须以字母后下划线开头,后面跟字母、下划线或者数字。

Requirements:

  1. 以字母或者下划线开头
  2. 后跟字母、下划线或者数字
  3. 只检查长度大于等于2的标识符
#!user/bin/env python

import string   # 导入string 模块

alphas = string.ascii_letters + '_'
nums = string.digits
alphanums = alphas + nums

print('Welcome to the Identifier Check version1.0')
print('Testees must be at least 2 characters long.')

My_Input = input('Identifier to test:')

if len(My_Input) > 1:                     # 判断是否有2个或以上字符
    if My_Input[0] not in alphas:        # 判断第一个字符是否为字母或者‘_’
        print('Invalid: first symbol mush be alphabetic or underscore')
    else:
        for otherChar in My_Input[1:]:     # 判断the rest字符是否满足要求
            if otherChar not in alphanums:
                print('Invalid: remaining symbols must be alphanumeric')
                break
            else:
                print('okay as an identifier')
View Code
原文地址:https://www.cnblogs.com/lg100lg100/p/7126048.html