Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 检查用户输入是否与预定义的单词模式匹配_Python_User Input - Fatal编程技术网

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

Python 检查用户输入是否与预定义的单词模式匹配,python,user-input,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 sente

我想用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中的任何单词,它也会打印“lookgood”。我的代码有什么问题?

将if子句更正为

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

如果序言中的第[1]句、第[2]句或第[3]句:
这是错误的(有人问了多少次这个问题?我不知道)这是阿加恩-让-弗朗索瓦法布说的,就一次。我将用户输入作为字符串,将其转换为列表,然后检查列表中是否包含过去形式的动词。@hashcode55我的收藏夹中有重复项,幸运的是…如果检查错误;你只是把句子[3]和前导词作比较;作为一个“或”检查,你的第一个检查,即句子[1]评估为真,因为它没有比较。这显然不是最好的解决方案:1)如果少于4个单词怎么办?2) 在python中,索引从0开始(3)在列表中使用
any
可能更像python。在回答这些糟糕的问题时,至少要努力改进代码,而不仅仅是指出明显的错误。@Jean-Françoisfare如果没有堆栈交换,我们会过多么悲惨的生活@我建议你阅读重复链接中的答案。真正学习python的方法和对常见错误的解释非常有启发性。@Jean-franoisfabre我感谢你的努力。随着时间的推移,我肯定会进步的。你知道,我刚刚开始学习Python。我会读你的建议,还有更多。一开始小小的成功其实并不小。