我研究了O‘’Reilly编程集体智能版的第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我不知道如何纠正这个错误。拜托救救我。
发布于 2016-03-04 15:11:44
Franny想要从DAL起飞,但是您的日程没有从DAL起飞的航班。
https://stackoverflow.com/questions/35799236
复制相似问题