I need to be able to change "X" of the my_list[X] value once reaching an if statement by adding a number to the value. Here’s a basic example of my code:
with open("*/file.txt") as f:
data = f.read()
my_list = data.splitlines()
print(my_list[1])
Result:
Line 1 of my_list
What I need:
if sky = blue
my_list[+8]
print(my_list[9]) #the result of my_list[1] +8
Resulting in
Line 9 of my_list
I have a total of 8 my_list[X] values, and I need to be able to add 8 to the [X] number to automatically select the next set of lines once the if statement is reached (kinda repeated myself there).
>Solution :
I think it would do the trick!
i=1
with open("*/file.txt") as f:
data = f.read()
my_list = data.splitlines()
while True:
print(my_list[i])
# do stuff
if "sky" == "blue":
i+=8
# MUST specify exit condition
But do not forget to specify the exit condition and account for "IndexError: list index out of range"!