练习1:检测变量名是否有效
#!/usr/bin/env python
#coding:utf-8
__author__ = "hahaha"
'''
@author:hahaha
@file:haha.py
@contact:769218703@qq.com
@time:7/01/10:18 AM
@desc:
'''
import string
alphas = string.letter + '_'
nums = string.didits
print "欢迎进入输入变量名检测".center(30,'#')
print "输入要检测的变量名要求不少于一个字符"
variable =raw_input("输入要检测的变量名:")
variable =hello
if len(variable) > 0:
if variable[0] not in alphas:
print "变量名开头必须为字母或下划线"
else:
for ochar in variable[1:]:
if ochar not in alphas +nums:
print "无效的变量名"
break
else:
print "%s 变量名合法" % variable
else:
print "变量名长度必须大于0"
练习2:输出英文句子的单词数
#!/usr/bin/env python
#coding:utf-8
__author__ = "hahaha"
'''
@author:hahaha
@file:haha.py
@contact:769218703@qq.com
@time:7/01/10:18 AM
@desc:
'''
sentence =raw_input("please input an english sentence:")
print len(sentence.split())
字符串联练习
最新推荐文章于 2024-11-12 10:10:40 发布