nonetype对象没有函数python 3

时间:2014-11-30 20:33:01

标签: python

我写了一个程序是python3,但我不明白为什么当我调用打印的函数时它会返回正确的输出但是当我在“find()”中调用相同的函数时,我有一个没有类型的对象。特别是当我在变量数据上使用键时,我在函数“find()”的第四行有错误。你能帮我理解发生了什么吗?谢谢

def printed(filename, day, time):
try:
    f = open(filename)
    lines = f.readlines()
    d = defaultdict(list)
    start = lines.index(day+"\n")
    if day == 'Monday\n' or day == 'Tuesday\n' or day == 'Wednesday\n' or day == 'Thursday\n' or day == 'Friday\n':
        stop = lines.index("Saturday\n")
    elif day == 'Saturday\n':
        stop = lines.index("Sunday\n")
    else:
        stop = len(lines)
    hour = time[0] + time[1]
    minutes = time[3:]
    for line in lines[start:stop]:
        line = line.strip(",")
        line = line.replace("\n","")
        line = line.replace(" ","")
        line = line.split(".")
        key = line[0]
        if len(line) == 2:
            d[key] += [line[1]]
    d = dict(d)
    print(d)
except IOError:
    print("File not found")
    program()

...

def find(filename, day, time):
    try:
        data = printed(filename, day, time)
        data2 = [int(h) * 60 + int(m) for h in data.keys() for m in data[h]]
        start_hour, start_minute = map(int, time.split('.'))
        start = start_hour * 60 + start_minute
        end = start + 30
        after = list(filter(lambda x: start <= x <= end, data2))
        if len(after) == 0:
            return "\nThere is no bus for this time"
        return list(map(lambda x: '%02d.%02d' % (x // 60, x % 60), after))
    except IOError:
        print("The file was not found")
        program()

这是printed()的输出:

'12': ['06', '36', '06', '36'], '13': ['06', '36', '06', '36'], 
'11': ['06', '36', '06', '36'], '21': ['05', '35', '06', '35'], 
'18': ['11', '26', '41', '56', '06', '36'], '06': ['11', '26', '41', '56', '35'], 
'15': ['06', '36', '56', '06', '36'], '19': ['11', '40', '06', '35'], 
'17': ['11', '26', '41', '56', '06', '36'], '22': ['05', '35', '06', '35'], 
'07': ['11', '26', '41', '56', '05', '35'], '09': ['06', '36', '06', '36'], 
'20': ['05', '35', '06', '35'], '14': ['06', '36', '06', '36'], 
'10': ['06', '36', '06', '36'], '16': ['11', '26', '41', '56', '06', '36'], 
'23': ['05', '35', '06', '35'], '08': ['11', '26', '41', '06', '36']}

1 个答案:

答案 0 :(得分:1)

因为你在print()函数的末尾放了一个打印件。要调用它,你必须返回而不是打印。变化:

print(d)

使用:

return d