문제

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
...
도움이 되었습니까?

해결책

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

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top