非类型对象没有函数python3

2024-04-16 09:16:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我用python3编写了一个程序,但是我不明白为什么当我调用printed函数时它会返回正确的输出,但是当我在find()中调用同一个函数时,我有一个no-type对象。尤其是在变量数据上使用键时,函数“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']}

Tags: or函数forindexiftimelinefind
1条回答
网友
1楼 · 发布于 2024-04-16 09:16:24

因为在printed()函数的末尾放了一个print。你得写一份报税表而不是打印。更改:

print(d)

使用:

return d

相关问题 更多 >