Выход из рекурсивного цикла в Python - PullRequest
1 голос
/ 15 августа 2011

У меня есть следующий код, в результате чего 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 ]

2 голосов
/ 15 августа 2011

Добавить условие для sort='position' и / или filter='newest'.

1 голос
/ 15 августа 2011

Где вы справляетесь filter=='newest'?Код будет зацикливаться вечно, если вы не добавите elif filter=='newest' и не сделаете что-то для этого случая.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...