我试图找到电影数据库中任何两个actor之间的分离度。 我成功了,当我达到我的基本情况,为1的分离度(即演员在同一部电影中的另一位演员),但我用递归查找所有其他程度的分离,我也得到:递归错误 - 分离度
我用runtime error: maximum recursion depth exceeded in cmp.
##gets file with movie information
f = open("filename.txt")
actedWith = {}
ActorList = []
movies = {}
actedIn = []
dos = 1
def getDegrees(target, base, dos):
for actor in actedWith[base]:
if target == actor:
print base, "has ", dos, " degree(s) of separation from ", target
return
dos = dos+1
for actor in actedWith[base]:
getDegrees(target, actor, dos)
for l in f:
##strip of whitespace
l = l.strip()
##split by where forward-slashes are
l = l.split("/")
##add the first "word" on the line to the database of movie names
movies = {l[0] : l[1:]}
for e in l[1:]:
if e in actedWith:
actedWith[e] = actedWith[e]+movies[l[0]]
else:
actedWith[e] = movies[l[0]]
base = raw_input("Enter Actor Name (Last, First): ")
target = raw_input("Enter Second Actor Name (Last, First): ")
getDegrees(target, base, dos)
文本文件可以在http://www.mediafire.com/?qtryvkzmuv5jey3
中找到为了测试基地的情况下,我使用:Bacon, Kevin
和Pitt, Brad
。
要测试其他人,我使用Bacon, Kevin
和Gamble, Nathan
。
不...递归问题仍然存在,不幸的是。我输入了一组不同的演员:培根,凯文和小坂,Masami。相同的递归错误。 返回不同名称的错误是由'打印基'造成的,具有“,dos”与“目标”分离的程度,通过创建一个新变量来存储原始基础,问题得到解决,尽管逻辑计数程度错误仍然存在。 –
@RMartin请检查您的实施。我只是测试了这个名字,它工作正常。 –
对不起,试试约翰逊,切丽。 我只是复制/粘贴你的代码,以确保;它似乎通过其中的许多搜索,然后返回运行时错误。 –