-1

The first part of the code is outputting and working correctly, but I am not getting any output from the part about the 2nd for loop. It doesn't give any error either. It's acting like the code doesn't see the for loop.

My main goal is to collect the output of print(record_line) and print(mostly_line) in the same array or output.

with open("music_list.txt") as file:
   music_tuple = ('ANK','IST')
   music_records = file.read().splitlines()

   for record_line in music_records:
      if record_line.startswith(music_tuple):
         print(record_line)

      elif record_line.startswish('|'):
           mostly_line = music_list.replace('|','')
           print(mostly_line)
     
      else: 
        continue
   
    mostly_line = []

    for t in range(len(mostly_line)):
      song_name = mostly_line[t][0]
      artist = mostly_line[t][1]
      info = record_line[t]
      print(song_name, artist, info)  # I can't see the output of this part
    
2
  • 1
    Please fix the typos in your code (e.g. record_line.startswish) and provide a minimal reproducible example that we can run and reproduce your issue. Mar 29, 2022 at 18:57
  • It is a good start that you posted code to us. But the code does not work. e.g. the object music_list is unknown. I would recommend to remove the file reading part and replace it with a simple string or list of lines with sample data. And do not mix double and single quotes. It is up to you which one you use but keep it consistent in your code.
    – buhtz
    Mar 29, 2022 at 19:03

2 Answers 2

3

you redefine mostly_line in this row:

mostly_line=[]

now it has zero elements, so nothing to loop over.

2
  • In the block above, I can mostly print from the line. I need to get the same content in the for loop below, how can I achieve this?
    – ALT
    Mar 29, 2022 at 18:57
  • @ALT leave mostly_line=[] out of your code.
    – warped
    Mar 29, 2022 at 18:58
1

You are not getting any output because len(mostly_line) is 0. mostly_line is an empty list, as set in the line immediately preceding the for loop.

Not the answer you're looking for? Browse other questions tagged or ask your own question.