2014-09-23 60 views
-3

你好,我有一个问题,使我的验证,强制用户输入5个单词,目前如果他们进入一个空间,他们可以去槽也。这里是我的代码:Python数据验证字

cntr = 0 
for x in range(len(this.val)): 
    if this.val[x]. == ' ' and not this.val[x-1] == ' ' and x != 0: 
     cntr = cntr + 1 

if cntr lt 4: 
    error(res.Q3error) 
+2

什么是'this.val'?你的代码是无效的python – 2014-09-23 23:07:08

+0

欢迎来到StackOverflow,为了得到答案,请阅读并遵循本[指南](http://stackoverflow.com/help/how-to-ask)。 – 2014-09-23 23:19:49

回答

0
words = sentence.split() 
if len(words) == 5: 
    print('good') 

或者可能添加一个“is_word”测试使用适用于“字”全() - 如果你想拒绝的数字什么的。