这是我的程序:
def FoundmovieNamesInQuery(userQueryPart,nameOfMovie):
for userQueryPart in userQuery:
for nameOfMovie in movieNames:
if userQueryPart == nameOfMovie:
return True
return False
print("welcome")
userQuery = input("use our quick search to find cinema times for films shwing today: ").lower().split()
print(userQuery)
with open("movies.txt")as file:
lines = file.readline()
slutionFound = False
for line in lines:
item = line.split(":")
movieNames = item[0].split()
movieTimes = item[1]
if FoundmovieNamesInQuery():
print(movieTimes)
solutionFound = True
if solutionFound == False:
print("movie not found.\n please call us on 0800 020 030")
但当我运行它时,它会给出以下错误消息:
welcome
use our quick search to find cinema times for films shwing today: annabelle
['annabelle']
Traceback (most recent call last):
File "C:\Users\FARUQE TALUKDAR\Downloads\online film.py", line 20, in <module>
movieTimes = item[1]
IndexError: list index out of range
这是我的文本文件:
run along : this movie will be showing at 18.00
annabelle : this movie will be showing at 13.00
x-men : this movie will be showing at 7.00
首先,您需要使用
readlines
而不是使用readline
,它将读取整个txt。你知道吗其次,需要将参数传递给函数
FoundmovieNamesInQuery(userQuery, movieNames)
。你知道吗相关问题 更多 >
编程相关推荐