我们可以编写一个Python函数来完成这个任务。首先,我们需要定义一个字符串变量来存储我们要检查的单词列表。接下来,我们需要将输入的句子拆分成单词,然后使用循环来检查每个单词是否在我们的列表中。
下面是代码示例:
def check_words(sentence):
# 定义要检查的单词列表
words_to_check = ['apple', 'banana', 'orange']
# 拆分句子成单词
words = sentence.split()
# 遍历每个单词
for word in words:
# 检查单词是否在列表中
if word in words_to_check:
print(word + " is a valid word.")
else:
print(word + " is not a valid word.")
您只需要把要检查的句子作为参数传递到函数中,它就会打印出每个单词是有效的还是无效的。
check_words("I want to eat an apple")
输出:
I is not a valid word.
want is not a valid word.
to is not a valid word.
eat is not a valid word.
an is not a valid word.
apple is a valid word.
注意,这个函数只是一个简单的示例,您可以根据具体的需求进行更改或扩展。