我从O' Reilly Programming Collective Intellegence学习了第5章,并在运行了部分代码之后:
import time
import random
import math
people = [('Seymour','BOS'),
('Franny','DAL'),
('Zooey','CAK'),
('Walt','MIA'),
('Buddy','ORD'),
('Les','OMA')]
# Laguardia
destination='LGA'
flights={}
file = open('schedule.txt','r')
for line in file:
origin,dest,depart,arrive,price=line.strip().split(',')
flights.setdefault((origin,dest),[])
# Add details to the list of possible flights
flights[(origin,dest)].append((depart,arrive,int(price)))
def getminutes(t):
x=time.strptime(t,'%H:%M')
return x[3]*60+x[4]
def printschedule(r):
for d in range(len(r)//2):
name=people[d][0]
origin=people[d][1]
out=flights[(origin,destination)][r[d]]
ret=flights[(destination,origin)][r[d]]
print('%10s%10s %5s-%5s $%3s %5s-%5s $%3s' % (name,origin,out[0],out[1],out[2],ret[0],ret[1],ret[2]))
s=[1,4,3,2,7,3,6,3,2,4,5,3]
printschedule(s)
带有文本文件(schedule.txt):
LGA,MIA,20:27,23:42,169
MIA,LGA,19:53,22:21,173
LGA,BOS,6:39,8:09,86
BOS,LGA,6:17,8:26,89
LGA,BOS,8:23,10:28,149
我收到错误:
out=flights[(origin,destination)][r[d]]
IndexError: list index out of range
我无法理解如何解决此错误。请帮忙。
答案 0 :(得分:1)
Franny
想要从DAL
起飞,但您的日程安排没有来自DAL
的任何航班。