无法在我的python代码中发现错误

时间:2017-06-21 15:41:55

标签: python

我正在学习一些python编码,我也跟着this youtube video做了一些练习。 现在我正在运行。

import urllib
import operator
from xml.etree.ElementTree import parse
import webbrowser
from showmap import showmap

def getbuses():
    u = urllib.urlopen('http://ctabustracker.com/bustime/map/getBusesForRoute.jsp?route=22')
    data = u.read()
    f = open('rt22.xml', 'wb')
    f.write(data)
    f.close()
    doc = parse('rt22.xml')
    running_buses = {}
    for bus in doc.findall('bus'):
        idbus = int(bus.findtext('id'))
        lat = float(bus.findtext('lat'))
        lon = float(bus.findtext('lon'))
        direction = str(bus.findtext('d'))
        running_buses[idbus] = {lat, lon, direction}
    return running_buses

def print_routes(running_buses):    
    print 'Running buses on route 22:\n'
    for b, (lat, lon, direction) in running_buses.items():
        print '  Bus number: {} \n     - Latitude: {} \n     - Longitude: {}\n     - Direction: {}'.format(b, lat, lon, direction)
    return

def breakline():
    print '____________________________________\n'
    return

def closest_bus():
    running_buses = getbuses()
    officelat, officelon = 41.980262, -87.668452
    diffs = []
    found = False
    print 'Office coordinates: lat {0} lon {1}'.format(officelat, officelon)
    for b, (lat, lon, direction) in running_buses.items():
        if lat > officelat:
            if direction.startswith('North'):
                diff = (officelat - lat) + (officelon - lon)
                diffs.append(tuple((abs(diff), b)))
                found = True
    if found == False:
        print 'No bus going north right now'
    else:
        closbus = min(diffs, key = operator.itemgetter(0))
    return closbus

def main():
    breakline()
    running_buses = getbuses()
    print_routes(running_buses)
    breakline()
    print 'Closest bus going north: {}'.format(closest_bus()[1])
    return

if __name__ == '__main__':
    main()

奇怪的是,对于一些公共汽车来说,程序在某种程度上混合了经度,纬度和方向,没有明显的理由。

例如,这就是我现在所拥有的:

root@kali:~/python_training/newtraining# python bussearch.py 
____________________________________

Running buses on route 22:

  Bus number: 1920 
     - Latitude: North West Bound 
     - Longitude: 41.9186187744
     - Direction: -87.6363869407
  Bus number: 1856 
     - Latitude: 41.8836083476 
     - Longitude: South Bound
     - Direction: -87.6310359701
  Bus number: 1764 
     - Latitude: South Bound 
     - Longitude: -87.6317800846
     - Direction: 41.9113892609
  Bus number: 1893 
     - Latitude: South Bound 
     - Longitude: 41.8746980631
     - Direction: -87.6310698311
  Bus number: 1882 
     - Latitude: 41.9857769012 
     - Longitude: -87.669216156
     - Direction: North Bound
  Bus number: 1911 
     - Latitude: 41.978255328 
     - Longitude: -87.6683738559
     - Direction: North Bound
  Bus number: 1400 
     - Latitude: -87.6738596316 
     - Longitude: 42.0089025851
     - Direction: North Bound
  Bus number: 1892 
     - Latitude: North West Bound 
     - Longitude: -87.6453846035
     - Direction: 41.933323362
  Bus number: 1914 
     - Latitude: -87.6671066284 
     - Longitude: South Bound
     - Direction: 41.9671321698
  Bus number: 1723 
     - Latitude: -87.6315689087 
     - Longitude: South Bound
     - Direction: 41.9017485594
  Bus number: 1885 
     - Latitude: South Bound 
     - Longitude: 41.9857135407
     - Direction: -87.6692754667    
____________________________________

Office coordinates: lat 41.980262 lon -87.668452
Traceback (most recent call last):
  File "bussearch.py", line 69, in <module>
    main()
  File "bussearch.py", line 65, in main
    print 'Closest bus going north: {}'.format(closest_bus()[1])
  File "bussearch.py", line 50, in closest_bus
    if direction.startswith('North'):
AttributeError: 'float' object has no attribute 'startswith'

我知道代码不是那么干净但我从3天开始编写python并且我正在学习。我对编码并不陌生,这对我没有任何意义...... 我究竟做错了什么?在介绍方向之前,一切似乎都很好。 有什么帮助吗?

1 个答案:

答案 0 :(得分:6)

在这一行running_buses[idbus] = {lat, lon, direction}中,您定义了一个集合,它是一个无序集合。因此,当您说for b, (lat, lon, direction) in running_buses.items()时,您在某些情况下会为direction变量分配一个纬度或经度。

将第一行更改为running_buses[idbus] = (lat, lon, direction)以改为使用(有序)元组。