编写干净的代码:在金字塔中嵌套For / If / Elif

时间:2015-11-20 20:48:14

标签: python if-statement for-loop pyramid

目标: 尝试打印待定结果Assessment.name,例如'Becoming a Leader''Something Else'等等 - 打印用户未在{{1}中完成的任何内容})和已完成的结果(来自all_assessments的{​​{1}},例如assessment.name - 打印用户已完成的内容(如Assessment_Results.assessment.name所示)干净,快速循环,必要的条件。

问题: 目前的代码没有达到上述目标。

任何建议都非常感谢!我还是个新手,所以任何指导都真的很受欢迎!

Views.py

'Becoming a Leader'

api的功能细分:

目前user_results打印出所有现有评估名称和文字的列表。

   def view_assessments(request):
        owner = authenticated_userid(request)
        print 'login owner', owner
        if owner is None:
            raise HTTPForbidden()
        all_assessments = api.retrieve_assessments()
        print 'these are all the assessments:', all_assessments
        print 'and type:', type(all_assessments)
        all_results = api.retrieve_assessment_results() # all the assessment results in a list

for x in all_assessments:
    alls = x.name
    if alls is not None: 
        for x in all_results: #found user based on all results
            assessment = x.assessment.name
            user = x.owner.username
            if user == owner:
                print 'completed', assessment
            elif assessment != alls: # DOES NOT WORK
                alls.index(assessment)

return {'assessments': all_assessments, 'assessment_results': all_results, 'loggedin': owner, 'user_results': user_results}

all_assessments打印出列表中每个用户的所有结果。如图所示:

all_assessments = [<Assessment(name='Becoming a Leader', text='better decisions')>, <Assessment(name='Good work', text='working on these skills')>, <Assessment(name='Teaching NTS', text='Series 1.1')>]

最后,all_results打印用户名找到的结果(基于登录的任何人)。

all_results [<Assessment_Result(owner='<User(username ='baseball', password='...', firstname ='Jenny', lastname ='Jen', email='dance@aol.com')>', assessment='<Assessment(name='Becoming a Leader', text='better decisions')>')>, <Assessment_Result(owner='<User(username ='donald', password='...', firstname ='Drew', lastname ='James', email='cool@gmail.com')>', assessment='<Assessment(name='Good work', text='working on these skills')>')>]

1 个答案:

答案 0 :(得分:1)

我会从这样的事情开始:

def view_assessments(request):
     logged_in_userid = authenticated_userid(request)
     if logged_in_userid is None:
         raise HTTPForbidden()
     all_assessments = api.retrieve_assessments()
     all_results = api.retrieve_assessment_results()

     completed_assessments = []
     pending_assessments = []

     for assessment in all_assessments:
         if assessment.name is None: 
             continue

         found_assessment_result = False
         for result in all_results:
             if result.owner.username == logged_in_userid and result.assessment == assessment:
                 found_assessment_result = True
                 break  # no need to check further

         if found_assessment_result:
             compleded_assessments.append(assessment)
         else:
             pending_assessments.append(assessment)


      return {'completed_assessments': completed_assessments, 'pending_assessments': pending_assessments, 'loggedin': owner, 'user_results': user_results}

在迭代两个嵌套列表时,这里的技巧是找到一个&#34;找到&#34; boolean,在进入内部循环之前设置为False - 在内部循环结束后,您可以检查变量,并根据其值将评估推送到两个列表中的一个。

正如您所怀疑的那样,此代码可能效率很低,因为它必须迭代所有评估和所有结果的产品,因此,如果您有10个评估和10个结果,则需要100次迭代,但如果您有100个评估和100个结果,它将是10.000次迭代。但它会做一个学习练习。