Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Change my_list[1] to my_list[9] by adding 8 when reaching an if statement

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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"!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading