代码:
def func1(str):
count = 0
for i in str:
if 'a' <= i <= 'z' or 'A' <= i <= 'Z':
count += 1
return count
s = str(input("请输入一个字符串:"))
count = func1(s)
print("字符串中单词的个数:%d" % count)
效果:
def func1(str):
count = 0
for i in str:
if 'a' <= i <= 'z' or 'A' <= i <= 'Z':
count += 1
return count
s = str(input("请输入一个字符串:"))
count = func1(s)
print("字符串中单词的个数:%d" % count)