Andromedae93 Andromedae93 - 1 month ago 9
Python Question

Returned result is false with numpy conditions

I wrote a little script in order to return a list of 'characters' from a list of values :

List of values :

11
11
14
6
6
14


My script :

# -*- coding:utf-8 -*-

import numpy as np

data = np.loadtxt('/Users/valentinjungbluth/Desktop/produit.txt',
dtype = int)


taille = len(data)

for j in range (0, taille) :

if data[j] == 1 or 2 or 3 or 11 or 12 or 13 :
print 'cotisation'

if data[j] == 14 :
print 'donation'

else :
print 'part'


What I need to get :

'cotisation'
'cotisation'
'donation'
'part'
'part'
'donation'


What I get :

cotisation
part
cotisation
part
cotisation
donation


I don't see where I made an error ..
If someone could read my script and maybe correct him ?

Thank you

Answer

Try:

for j in range(0, taille):
    if data[j] in {1, 2, 3, 11, 12, 13}:
        print ("cotisation")
    elif data[j] == 14:
        print ("donation")
    else:
        print ("part")