200-901 · Question #401
200-901 Question #401: Real Exam Question with Answer & Explanation
The user needs to correctly complete a Python add_member method within a Team class using provided code snippets to manage a list of members and return appropriate boolean values, particularly for a 'happy path' scenario in a Pytest context.
Question
Drag and Drop Question Drag and drop the code snippets from the bottom onto the blanks in the code to complete the happy path scenario. Not all options are used. Answer:
Explanation
The user needs to correctly complete a Python add_member method within a Team class using provided code snippets to manage a list of members and return appropriate boolean values, particularly for a 'happy path' scenario in a Pytest context.
Approach. The add_member method intends to add a member to the self.members list if the member is not already present, and return True for success. If the member is already present, it should not add the member and return False.
- For the first blank (inside the
if member not in self.members:block): This is the 'happy path' where a new member needs to be added. The correct Python idiom to add an item to a list (whichself.membersis) islist.append(item). Therefore, the snippetself.members.append(member)should be dragged to the first blank. This action ensures the member is added to the team's internal list. - For the second blank (inside the
else:block): This block is executed ifmemberis already inself.members. The method should indicate that the member was not added (because it already exists) by returningFalse. The original code structure hasreturn Falsefollowing the blank. In drag-and-drop questions where a specific statement is intended for a blank, even if areturnis implicitly there, dragging the explicitreturn Falsesnippet to the blank is the correct interaction, effectively making the blank thereturnstatement for that branch. This completes the logic for the case where the member already exists, ensuring the method correctly returnsFalse.
Common mistakes.
- common_mistake. 1. Dragging
team.add_member(member)to the first blank: This is a common mistake related to misunderstanding theselfkeyword in Python OOP. Inside an instance method likeadd_member,selfrefers to the current instance of theTeamclass.teamis a variable name used in the Pytest fixture, not a general reference to the current object within the method's scope. Making this call would either result in aNameErroror, ifteamsomehow existed in a closure (which it doesn't here), it would call a method on a differentTeamobject. The correct way to modify the current object'smemberslist isself.members.append(member).
- Dragging
yield member:yieldis used in generator functions or Pytest fixtures to provide values iteratively or set up/tear down resources, not for adding elements to a list in a regular method. - Dragging
member.add_member(): Thememberobject (an instance ofm.Member) does not have anadd_membermethod defined in the provided code, and this action would not contribute to adding the member to theTeam's list of members. - Dragging
return Trueorreturn Falseto the wrong blank: Whilereturn Trueis part of the happy path andreturn Falseis part of the unhappy path, they must be placed in the correct logical branch. Usingreturn Truein theelseblock, orreturn Falsein theifblock after successfully adding a member, would completely break the method's intended logic.
Concept tested. Python object-oriented programming (OOP) principles, instance methods and the self keyword, list manipulation (appending and checking membership), conditional logic (if-else statements), and method return values.
Topics
Community Discussion
No community discussion yet for this question.