检查用户输入是否与预定义的单词模式匹配

时间:2017-03-01 16:06:09

标签: python user-input

我想用Python制作一个语法检查器。但这不是一个标准的语法检查器。这适用于初级水平的英语学习者。我编写了以下代码来检查用户是否使用了正确的动词形式:

preList = ['came', 'waited', 'sat', 'stood', 'went', 'left']
sentence = input("Type a sentence using the past simple tense: ")
sentence = sentence.split()
if sentence[1] or sentence[2] or sentence[3] in preList:
print("Looks good!")
else:
print("Maybe you're missing something!") 

我遇到的问题是,即使用户输入不包含preList中的任何单词,它也会打印出“看起来不错”。我的代码有什么问题?

1 个答案:

答案 0 :(得分:0)

将if子句更正为

if sentence[1] in preList or sentence[2] in preList or sentence[3] in preList: