user583088
user583088

Reputation: 1085

if condition does not executed in python after reading values from text file

I dont understand why my if condition is not getting executed in python.` values inside my text file are

 0.000      0 
 0.001      1 
 0.002      2 
f = open(sys.argv[1],"r").readlines()
var=0

for line in f:

    new = f[var].split()

    Time = new[0]
    rev=float(new[1])

    var=var+1

    if 0.001 > Time :

            print " I am here "

Upvotes: 0

Views: 100

Answers (1)

Lev Zakharov
Lev Zakharov

Reputation: 2427

Guess you should rewrite your code:

filename = sys.argv[1]
with open(filename) as f:
    for line in f:
        time, rev = map(float, line.split())
        if time < 0.001:
            print("I'm here")

You tried to compare string (Time variable) with float (0.001) - it's wrong. In python 2 it's ok, but always False. I recommend you to start using python 3 - you can't compare floats with strings with this version:)

Upvotes: 2

Related Questions