+ 1
if statement doesn’t work
I wanna get some data which match the given condition. But the print shows that all rows ran in if statements. How did that run like this?
6 Antworten
+ 3
@Ace seems to be right. And the if statement expressions seem to be guaranteed to evaluate to true.
I feel like the statement should be
if (((114.028 <= old_matrix[row,1]) AND (old_matrix[row,1]) <= 114.029)) OR ((22.5555 <= (old_matrix[row,2]) AND (old_matrix[row,2] <= 22.5565))):
Which means
if row 1 is between 114.028 and 114.029
OR
If row 2 is between 22.5555 and 25.5556
Then do something
Before it was saying if row1 is below or equal to X or above or equal to (X+.001) [Which is almost always true unless row1 is between 114.0281 and 114.0289]
AND
Row2 similarly matches criteria that is almost always true then do something
Depending on the intended outcome of the code, but I am guessing a human language, like English caused the logic operators And/ Or to be written in the wrong place leading to a result opposite of the desired outcome
https://code.sololearn.com/cadBe1XJhwde/?ref=app
+ 1
https://code.sololearn.com/chxin0AGpcxw/?ref=app
+ 1
@SQrl that’s the problem! thx~☺️
+ 1
you must give the code along with your question
0
def velocity_cal(old_file,new_file):
with open(old_file,'r') as old_csvFile:
old_matrix = numpy.loadtxt(old_csvFile, delimiter=",", skiprows=0)
rows = len(old_matrix)
new_csvFile = open(new_file, 'w')
writer = csv.writer(new_csvFile)
for row in range(0,65500,1):
if (((114.028 <= old_matrix[row,1]) or (old_matrix[row,1]) <= 114.029) and (22.5555 <= (old_matrix[row,2]) or (old_matrix[row,2] <= 22.5565))):
print(row)
writer.writerow(old_matrix[row,1])
row = row + 65500
rows = rows - 65500
0
That doesn’t work😭but thk u very much