跳至內容
選單
此問題已被標幟
3 回覆
3844 瀏覽次數

I'm tryng to validate a field, so I've written this code:

        @api.one
@api.constrains('code')
def control_code(self):
i=0
a=1
while i<9:
if i==0:
if unicode.isdigit(self.code[0]):
a=i+1
raise Warning(("Warning!\nThe character %s must be a letter") % (a))
elif i in range (1,4) or i in range (5,10):
if not unicode.isdigit(self.code[i]):
raise Warning(('Warning!\nThe character %s must be a number') % (a))
elif i == 4:
if self.code[i] is not '/':
raise Warning(("Warning!\nThe character %s must be '/'") % (a))
i=i+1
a=a+1

Everything works fine, except for i==4. Even if I put a '/', I get the warning message.

Anyone knows why?

頭像
捨棄
最佳答案

Hi friend:

Try this

if self.code[i] !=  '/':

raise Warning(("Warning!\nThe character %s must be '/'") % (a))

Use != (preferred) or <> (deprecated). See comparison operators : https://docs.python.org/release/2.5.2/lib/comparisons.html. For comparing object identities, you can use the keyword is and its negation is not.

Regards.


頭像
捨棄
作者

It works! Thank you!

Happy for you :) You Re WELCOME

最佳答案

Maybe because you are checking if self.code[i] is not '/' and raising the error based in var a, when:

i = 4

a = 5

Maybe you expect some string in code like:

'http://www'

For something like this you need to check:

if self.code[a] is not '/':


頭像
捨棄
最佳答案

Try

if self.code[i] != chr(47):



頭像
捨棄
相關帖文 回覆 瀏覽次數 活動
0
4月 24
1883
4
11月 23
6003
0
10月 23
1756
0
12月 22
2644
2
12月 23
19368