2013-02-21 36 views
0

喜有誰能夠告訴我,我怎麼能檢查變量值在數組或不 像我有檢查變量值在數組或不Django的

variable = 17.40 
array = [14.40,14.12,45.50.....] 

需要檢查變量的值存在或不存在

編輯 我有嘗試以下方法,但它亙古不變的工作

scoremx = [19,18,17] 
style_score=score.objects.get(user_id=request.user.id) 
if style_score.style_quiz_score in scoremx: 

it goes in else cxondition but it has the 19 value in database 
+0

'style_quiz_score'是一個整數嗎? – Zulu 2013-02-21 11:19:36

+0

包含float和int值 – 2013-02-21 11:20:10

回答

3

試試這個:

if int(style_score.style_quiz_score) in scoremx: 
    pass 

你不能比較int和float。 你應該這樣做:

if 17 <= style_score.style_quiz_score < 20: 
    pass 
+0

非常完美,非常感謝 – 2013-02-21 11:32:38

+0

'你不能比較int和float' - 錯誤;在Python中,你可以檢查列表中是否有元素,例如:if'test'in [1,2.4,'test']:print'ok' – 2013-02-21 11:37:25

0
if variable in array: 
    #do something 
+0

我已嘗試以下方法,但它不起作用scoremx = [19,18,17] style_score = score.objects.get(user_id = request.user.id) 如果scoremx中的style_score.style_quiz_score:它進入其他方式,但它在數據庫中有19的值 – 2013-02-21 11:13:45

+0

什麼是style_score.style_quiz_score的類型? – 2013-02-21 11:15:52

+0

包含浮點數和int值 – 2013-02-21 11:17:09