|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#零基础学python第19课的作业
#定义函数
def count(*str1):
"检索字符串各种字符的数量"
times = 0
for i in str1:
eng,num,space,other = 0,0,0,0
for k in i:
if k.isalpha():
eng += 1
elif k.isdecimal():
num += 1
elif k == " ":
space += 1
else:
other += 1
times += 1
print('第%d个字符串共有:英文字母%d个,数字%d个,空格%d个,其他字符%d个。' % (times,eng,num,space,other))
#运行
temp = 0
list1 = []
while temp != "over":
temp = input('请输入要检索的字符串(输入over结束):')
if temp != 'over':
list1.append(temp)
count((a for a in list1))
我估计最后一句是不对的?请问如何书写?
用星号解包。
- #零基础学python第19课的作业
- #定义函数
- def count(*str1):
- "检索字符串各种字符的数量"
- times = 0
- for i in str1:
- eng,num,space,other = 0,0,0,0
- for k in i:
- if k.isalpha():
- eng += 1
- elif k.isdecimal():
- num += 1
- elif k == " ":
- space += 1
- else:
- other += 1
- times += 1
- print('第%d个字符串共有:英文字母%d个,数字%d个,空格%d个,其他字符%d个。' % (times,eng,num,space,other))
- #运行
- temp = 0
- list1 = []
- while temp != "over":
- temp = input('请输入要检索的字符串(输入over结束):')
- if temp != 'over':
- list1.append(temp)
- count(*list1)
复制代码
|
|