简答题
偏难
#15321
统计单词 统计英文文本中出现的不同单词个数:输入一段英文文本,要求单词和单词之间用1个空格或标点符号隔开,文末以标点符号结尾,在区分单词大小写的情况下,输出该文…
统计单词
统计英文文本中出现的不同单词个数:输入一段英文文本,要求单词和单词之间用1个空格或标点符号隔开,文末以标点符号结尾,在区分单词大小写的情况下,输出该文本中所出现的不同单词个数。Python程序运行界面如下所示:
请输入英文文本:Where there is life,there is hope.
出现的不同单词个数为: 5
实现上述功能的 Python 程序如下,请在划线处填入合适的代码。
text=input("请输入英文文本:")
lst=[]
s=""
def judge(st):
if ① :
return False
else:
return True
for i in range(len(text)):
②
if "a"<=c<="z" or "A"<=c<="Z":
③
else:
if judge(s):
lst.append(s)
s=""
print("出现的不同单词个数为:", ④ )
答案与解析
正确答案:<pre class="brush:python;toolbar:false">text=input("请输入英文文本:")
lst=[]
s=""
def judge(st):
if st in lst:
return False
else:
return True
for i in range(len(text)):
c=text[i]
if "a"<=c<="z" or "A"<=c<="Z":
s=s+c
else:
if judge(s):
lst.append(s)
s=""
print("出现的不同单词个数为:",len(lst))</pre>
解析
评分标准:
(1)st in lst(或等效答案);(2分)
(2)c=text[i](或等效答案);(2分)
(3)s=s+c(或等效答案);(3分)
(4)len(lst)(或等效答案)。(3分)