退出Python中的递归循环

时间:2011-08-15 01:45:32

标签: python django

我有以下代码,导致Python“意外退出”:

def profile_videos_sort_and_filter(profile, sort, filter, uncredited_videos, list_of_credits=[]):
    """Given a sort and filter, this will order the credits and return a credit_set."""
    if filter == 'user' or filter == 'all':
        if filter == 'user':
            credit_set = profile.videocredit_set.filter(video__uploaded_by=profile)
        if filter == 'all':
            credit_set = profile.videocredit_set.all()

        if sort == 'alphabetical':
            credit_set = conform_videos_and_credits(credit_set, uncredited_videos, sort)
        if sort == 'newest':
            credit_set = conform_videos_and_credits(credit_set, uncredited_videos, sort)
        if sort =='position':
            credit_set = credit_set.order_by('position')
            list_of_credits = position_credit_set(profile, credit_set, filter, uncredited_videos)

    elif filter == 'others':
        credit_set = profile.videocredit_set.exclude(video__uploaded_by=profile)  
        if sort == 'alphabetical':
            credit_set = credit_set.order_by('video__title')
        if sort == 'newest':
            credit_set = credit_set.order_by('video__uploaded_at')
        if sort == 'position':
            credit_set = credit_set.order_by('position')
            list_of_credits = position_credit_set(profile, credit_set, filter, uncredited_videos)

    #### THIS IS THE PART THAT CAUSES THE ERROR ###
    else:
        ### if none of the above, call the function, passing default parameters ###
        profile_videos_sort_and_filter(profile, uncredited_videos=uncredited_videos,
                                        sort='position', filter='newest')

    return credit_set, list_of_credits

如何将最终的else子句更改为有效?谢谢。

2 个答案:

答案 0 :(得分:2)

sort='position'和/或filter='newest'添加条件。

答案 1 :(得分:1)

你在哪里处理filter=='newest'?代码将永远循环,除非您添加elif filter=='newest'并对该案例执行某些操作。