我試圖找到電影數據庫中任何兩個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請檢查您的實施。我只是測試了這個名字,它工作正常。 –
對不起,試試約翰遜,切麗。 我只是複製/粘貼你的代碼,以確保;它似乎通過其中的許多搜索,然後返回運行時錯誤。 –