2014-09-23 61 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」測試使用適用於「字」全() - 如果你想拒絕的數字什麼的。