import string searchstring = "1990" limit = 5 def main(): # Reads file top250.txt and prints the lop limit movies in the top 250 ranking # for the year specified in searchstring; # quits reading the file after limit movies have been found (done in an incorrect way) infile = open("top250.txt") infile.readline() infile.readline() infile.readline() total = 0 print "Top", limit, "movies of", searchstring while total < limit: # read the next line in file and split in spaces line = infile.readline() fields = line.split(" ") # extract the year of the movie length = len(fields) lastField = fields[length-1] year = lastField[1:5] if year == searchstring: total = total + 1 # print not as list, but as a string looking more like the input line = "" for elem in fields: line = line + " " + elem print line infile.close() main()