【发布时间】:2021-07-30 08:26:21
【问题描述】:
我得到了一本包含以下单词的字典:
dictionary = ['all', 'an', 'and', 'as', 'closely', 'correct', 'equivocal',
'examine', 'indication', 'is', 'means', 'minutely', 'or', 'scrutinize',
'sign', 'the', 'to', 'uncertain']
我正在尝试编写一个拼写检查器来判断输入句子中的哪些单词拼写错误。
预期的输出是:
A) 在新行打印拼写错误的单词
或
B) 只有在所有单词拼写正确时,才可以打印
这是我目前想出的代码:
dictionary = ['all', 'an', 'and', 'as', 'closely', 'correct', 'equivocal',
'examine', 'indication', 'is', 'means', 'minutely', 'or',
'scrutinize', 'sign', 'the', 'to', 'uncertain']
text = input().split()
counter = 0
for x in text:
if x not in dictionary:
counter += 1
print(x, end='\n')
elif not counter:
print("OK")
给出了两个示例输入和输出作为预期结果的示例:
示例输入 1:
srutinize is to examene closely and minutely
样本输出 1:
srutinize
examene
示例输入 2:
all correct
示例输出 2:
OK
代码对于输入 1 工作正常,但输入 2 而不是打印 OK 是打印 all correct 而不是 OK。
【问题讨论】:
-
刚刚更新了帖子。真的需要一些指导,拜托。
标签: python loops split list-comprehension any