how to iterate through each set

Cameron Walsh cameron.walsh at gmail.com
Sun Nov 4 03:59:05 EST 2007


Tom_chicollegeboy wrote:
> I figured out problem. here is my code. now it works as it should!
> Thank you everyone!
>
I decided my 4th clue earlier was too much, so I removed it before 
posting.  It looks like you got it anyway =)

You've now solved it the way the course instructor intended you to solve 
it.  I would have done it this way:

def speed():
     journeys = []
     for line in open('speed.in', 'r').readlines():
         line = list(int(i) for i in line.split())
         if len(line) == 1:
             if line[0] == -1:
                 break
             time = 0
             journeys.append(0)
         else:
             journeys[-1] += (line[1]-time) * line[0]
             time = line[1]
     for journey in journeys:
         print journey

speed()



More information about the Python-list mailing list