1

I'm not understanding why == condition is not working but != is working in for loop. Here's the code segment:

# this way it's not working . only single time running and not giving desired output

list_of_student= []

for student in student_list:

    student_detail = student(val[1],val[2],val[3])  # namedtuple

    if (student_id and student_id==student.id) or (student_name and student_name==student.name):
        return student_detail
    else:
        list_of_student.append(student_detail)

But if I change == to != and revert the following actions , it's working fine. Could you please tell me the reason, or, where I'm wrong ?

#this way it's working fine.
list_of_student= []

for student in student_list:

    student_detail = student(val[1],val[2],val[3])   # namedtuple
    if (student_id and student_id!=student.id) or (student_name and student_name!=student.name):
        list_of_student.append(student_detail)

    else:
        return student_detail
Vincent Savard
  • 34,979
  • 10
  • 68
  • 73
SamCodes
  • 384
  • 4
  • 17
  • WHy did you do student_id and student_id?? Why not just do `student_id == student.id` –  Dec 03 '15 at 16:16
  • Sure you didn't mean `student_detail.id` and `student_detail.name`? – tzaman Dec 03 '15 at 16:17
  • The first check in each parenthesis was probably meant to ensure that attribute access was not done on `None` objects, in order to avoid exceptions. If indeed it is trying to avoid this, the checks should be more specific: `student_id is not None and student_id != student.id`. – taleinat Dec 03 '15 at 17:10
  • I haven't uploaded the full coding, student_id is input to a fn. and we are checking with saved data in a file like student_detail.id or student.id – SamCodes Dec 03 '15 at 17:12
  • @tzaman , DeliriousSyntax: yeah , you are right, it's "student_detail.name" and in actual code it's right. But for == condition neither returning the correct value nor store in list. – SamCodes Dec 03 '15 at 17:26

2 Answers2

3

To reverse the logic of a condition, you need to also replace and and with or and vice-versa, as well as negating any boolean checks, in addition to reversing the comparison operators:

if ((not student_id) or student_id != student.id) and ((not student_name) or student_name != student.name):
taleinat
  • 8,441
  • 1
  • 30
  • 44
  • If this answers your question, please accept this answer! – taleinat Dec 03 '15 at 17:13
  • Yes, you are absolutely right, but here what I have to do , to check student_id (a given input ) is there and if it's matching with student_detail.id (here in question i have written wrong,but main code it's ok) , then return it ,else, add the student_detail(from loop) in a list. same with student_name. Postive logic not working but the negative one. – SamCodes Dec 03 '15 at 17:34
  • One more thing, request you to post in positive condition which is not working for me. – SamCodes Dec 03 '15 at 17:42
  • You must ask one specific thing in each question, not "help me get this entire piece of code working". I answered the specific question you asked about the difference between the two conditions; if you have additional questions, post them separately. – taleinat Dec 03 '15 at 21:38
0

You said it is stopping unexpectedly, which is because of the return in your for-loop, which is stopping the loop. Get rid of that and it should work (you can substitute it with pass).

Source: return statement in for loops

Community
  • 1
  • 1
Hybrid
  • 6,741
  • 3
  • 25
  • 45
  • yes, but if the condition fails, no use of return , right? But still it stops . Neither retrun correct value nor save in list. Please correct me , if any wrong. – SamCodes Dec 03 '15 at 17:21