我是靠谱客的博主 贪玩龙猫,这篇文章主要介绍Python 基础 - Day 2 Practice Example - String - 标识符检测 ID check,现在分享给大家,希望可以做个参考。
Name: 标识符检查 ID CHECK
Purpose: 用来检查python有效标识符的小脚本。python的标识符必须以字母后下划线开头,后面跟字母、下划线或者数字。
Requirements:
- 以字母或者下划线开头
- 后跟字母、下划线或者数字
- 只检查长度大于等于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')
转载于:https://www.cnblogs.com/lg100lg100/p/7126048.html
最后
以上就是贪玩龙猫最近收集整理的关于Python 基础 - Day 2 Practice Example - String - 标识符检测 ID check的全部内容,更多相关Python内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复