-3

Here is my code:

variables = [0, 0, 0, 0]

while (variables[2]<4):
    with open('vystup_online.txt') as f:
        for line in f:
            variables = (line.strip().split())
    print variables[2]

and here's my .txt file:

2 10.249402 0.000000 25.596176

I was assuming it will print variables[2] until I change the file to:

2 10.249402 1.000000 25.596176

or similar.

I am just getting

>>>
0.000000

and nothing more. What's wrong?

This is what I need:

>>>
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
...
TomasJ
  • 289
  • 8
  • 21
  • 2
    What are you actually trying to do? Do you want to watch a file for changes? This sounds somewhat like an [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – thegrinner May 12 '14 at 14:00

2 Answers2

1

variables[2]<4 will always be False at the second iteration.

since variables[2] is a string after the line variables = (line.strip().split()) and 4 is an int

and in python 2.x while string < int will result as False

you can read more about your mistake here

Community
  • 1
  • 1
Kobi K
  • 7,743
  • 6
  • 42
  • 86
  • sure...didn't notice that. It should look like this: `while (float(variables[2])<4):` now it works. – TomasJ May 12 '14 at 14:07
1

variables[2] is a string. '0.000000' < 4 is false.

njzk2
  • 38,969
  • 7
  • 69
  • 107