首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编程集体智能第5章示例不起作用

编程集体智能第5章示例不起作用
EN

Stack Overflow用户
提问于 2016-03-04 15:04:15
回答 1查看 48关注 0票数 0

我研究了O‘’Reilly编程集体智能版的第5章,并在运行了部分代码之后:

代码语言:javascript
复制
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):

代码语言:javascript
复制
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

我收到一个错误:

代码语言:javascript
复制
out=flights[(origin,destination)][r[d]]
IndexError: list index out of range

我不知道如何纠正这个错误。拜托救救我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-04 15:11:44

Franny想要从DAL起飞,但是您的日程没有从DAL起飞的航班。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35799236

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档