|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
1. 编写一个函数,分别统计出传入字符串参数(可能不只一个参数)的英文字母、空格、数字和其它字符的个数。
我的答案:
def count_cata(strs):
count_en = count_sp = count_dg = count_os = 0
for char in strs:
if char in string.ascii_letters:
count_en += 1
elif char.isspace():
count_sp += 1
elif char.isdigit():
count_dg += 1
else:
count_os += 1
return ("英文字母",char_en,"个","数字",char_dg,"个","空格",char_sp,"个","其他字符",count_os,"个。")
def count(*input_str):
t = 0
for strs in input_str:
count_cata(strs)
t += 1
print("第",t,"个字符串共有: ",count_cata(strs))
count("I love fishc.com.","I love you, you love me.")
返回错误:
File "<ipython-input-30-3f97949c927e>", line 13
elif char.isspace():
^
SyntaxError: invalid syntax
请问是为什么? |
|