0

Hi can anybody tell me how can i check the variable value is in array or not like i have

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

need to check the variable value is present or not

edited i have try the following but it doesnot work

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
Rohit Goel
  • 3,396
  • 8
  • 56
  • 107

2 Answers2

3

Try this:

if int(style_score.style_quiz_score) in scoremx:
    pass 

You can't compare an int and a float. And you should make like this :

if 17 <= style_score.style_quiz_score < 20:
   pass
Zulu
  • 8,765
  • 9
  • 49
  • 56
  • 'you can`t compare an int and a float' - wrong; in Python you can check if any element is in list, for example: if 'test' in [1, 2.4, 'test']: print 'ok' – Lukasz Koziara Feb 21 '13 at 11:37
1
if variable in array:
    #do something
Lukasz Koziara
  • 4,274
  • 5
  • 32
  • 43