Python 基础 - Day 2 Practice Example - String - 标识符检测 ID check

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值