如何在我的python代码中避免此错误

时间:2019-05-15 13:35:55

标签: python python-2.7

我的程序接受以下输入 1.我到达的时间 2.电影数量(n) 3.在电影的最后n行加上各自的小时价格和名称:

示例:

18:00
4
12:34 16 Overlord
18:40 15 Bohemian-Rhapsody
19:20 19 Elliot-O
21:22 21 Venom 

对于输出,它需要提供更接近我到达时间的电影。我使用下面的代码以分钟为单位,但出现以下错误:

hours=movie.split(' ')[0].split(':')[0]
AttributeError: 'builtin_function_or_method' object has no attribute 'split'

我尝试用strip(':')替换split(':'),同样的错误

movie=[]
for i in range(nr_movies):
    movies.append(raw_input)

dictionary_movies={}
for movie in movies:
    hour=movie.split(' ')[0].split(':')[0]
    minutes=movie.split(' ')[0].split(':')[1]
    price=int(movie.split(' ')[1])
    name=str(movie.split(' ')[2])
    #transform in minutes
    timestamp_movie=int(hours)*60+int(minutes)
    difference=timestamp_movie - timestamp 

2 个答案:

答案 0 :(得分:3)

您使用的是功能列表raw_input,而不是实际的输入。

替换此:

movies.append(raw_input)

与此:

movies.append(raw_input())

答案 1 :(得分:1)

# Consider these are the input values
arriving_hour = "18:00"
movies=[
    "12:34 16 Overlord",
    "18:40 15 Bohemian-Rhapsody",
    "19:20 19 Elliot-O",
    "21:22 21 Venom"
]

diff = []
for movie in movies:
    hour=movie.split(' ')[0].split(':')[0]
    minutes=movie.split(' ')[0].split(':')[1]
    price=int(movie.split(' ')[1])
    name=str(movie.split(' ')[2])
    timestamp_movie=int(hour)*60+int(minutes)
    arriving_minutes = int(arriving_hour[0:2])*60+int(arriving_hour[3:5])
    difference = abs(timestamp_movie - arriving_minutes)
    diff.append(difference)

min_diff = min(diff)
min_diff_index = diff.index(min_diff)

# This movie is closer to arriving hour
movies[min_diff_index]