A
A
aftnia112021-09-05 17:25:08
Python
aftnia11, 2021-09-05 17:25:08

Why doesn't Python remember the value from the list after exiting the loop?

There is a code, approximately with a similar design. It loops through the lines from the txt file. But the problem is that when the code exits the second loop, this second loop, after re-entering it, starts iterating over the values ​​from the beginning.

with open("Logins.txt", "r") as file:
  Log = file.readlines()
with open("FirstStream.txt", "r") as file:
  FStream = file.readlines()
i_Log=0
q_Stream=0
for i_Log in range(len(Log)):
   print(Log[i_Log])
    for q_Stream in range(len(FStream[q_Stream])):
        try:
            print(FStream[q_Stream])
            q_Stream+=1
        except:
            break
i_Log+=1

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-09-05
@Vindicar

And what's wrong?
At each iteration of the outer loop, the nested loop is executed "as for the first time" - a new range(len(FStream[q_Stream])) object is generated, and this range is iterated from zero to the specified value.
That's how it should work.
If you need something else, please explain what it is.
If you need to iterate over the lines of two files in parallel, then either use file.readline() to read individual lines, change the indexes manually, or use the standard zip() function.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question