Reputation: 6138
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
Upvotes: 0
Views: 70
Reputation: 3555
Scimonster point out your 1st issue in the code, you need elif for the second if.
The 2nd issue is the or part, your if condition is being intepret as
if (data[j] == 1) or 2 or 3 or 11 or 12 or 13
You could fixed them as below
if data[j] in [1, 2, 3, 11, 12, 13] :
print 'cotisation'
elif data[j] == 14 :
print 'donation'
else :
print 'part'
Upvotes: 0
Reputation: 5518
There are a couple issues here:
if x == 1 or 2 or 3 or ...
does not do what you are expecting.Each clause is evaluated independently, so python does not know you are implying if x == 1 or x == 2 or x == 3
, etc. It instead interprets this as if x == 1
or if 2
or if 3
... if 2
and the like is actually a sensical if statement in python and is basically tantamount to asking if True
(all non-zero integer values have a "truthiness" of True). So your statement is really equivalent to if data[j] == 1 or True or True or True or True
, which reduces to if True
. In other words, the condition is always satisfied regardless of the value. What you probably meant was:
if data[j] == 1 or data[j] == 2 or data[j] == 3 or data[j] == 11 ...
Currently boils down to something like the following in pseudocode:
if some condition:
print 'something'
now regardless of the first condition, if some other disjoint condition:
print 'something else'
otherwise:
print 'does not match other condition'
In other words, by using two ifs, the second if block, of which the else
is a part, is treated as completely independent of the first if. So cases that satisfy the first condition could also satisfy the else, which is sounds like is not what you wanted as each character should print exactly once. If you want it to be 3 disjoint cases, you need to use elif
instead so it is all considered part of a single block:
E.g.
if condition:
print 'something'
elif other condition:
print 'something else'
else:
print 'does not match EITHER of the two above condtions'
Upvotes: 1
Reputation: 132
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")
Upvotes: 1
Reputation: 33409
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'
You have two separate if
s here, and one has an else
. They should all flow together, so the second should be elif
.
Upvotes: -1