0

I have 3 lists:

members = ['Alex', 'Danny', 'Kieran', 'Zoe', 'Caroline']
visitors = ['Scott', 'Helen', 'Raj', 'Danny']
signups = ['Helen', 'Scott']

The function should return a list of visitors who are neither existing nor new members. If there are none, return None.

This is my code:

def guests(member,signup,visitor):
    visit = []
    for person in visitor:
        if person not in member and signup:
            visit.append(person)
            return visit
        return print("None.")

but my output returns ["Scott"]. I can't seem to figure what i haven't done right.

5 Answers 5

1

Firstly you return after the first value you get and secondly you might wanna change if person not in member and signup to if person not in member and person not in signup

members = ['Alex', 'Danny', 'Kieran', 'Zoe', 'Caroline']
visitors = ['Scott', 'Helen', 'Raj', 'Danny']
signups = ['Helen', 'Scott']
def guests(member,signup,visitor):
    visit = []
    for person in visitor:
        if person not in member and person not in signup:
            visit.append(person)
    return visit
print(guests(members,signups,visitors))
Sign up to request clarification or add additional context in comments.

Comments

1

person not in member and signup is evaluated as (person not in member) and (signup).

When person is 'Scott', the first part is True, and the second part (signup) is a non-empty list, so it is evaluated as True in boolean context, so the whole condition is True.

Then, you return immediately the output without further looping.

You meant:

def guests(member,signup,visitor):
    visit = []
    for person in visitor:
        if person not in member and person not in signup:
            visit.append(person)
    return visit if len(visit) != 0 else None

1 Comment

instead of if len(visit) != 0 it's better to use just if visit as per PEP8 recommendations. Although if it was my function I would return empty list, not None.
0

There is two things wrong here.

First

if person not in member and signup:

This interpreted by python in two steps. "person not in member" and then "signup", signup is "True" as long as signup is not empty. So it will trigger also on 'Scott'.

Instead you want:

if person not in member and person not in signup:

Second your return visit is inside the loop, so it will return the list as soon as the first element is discovered.

You need something like:

return visit if visit else "None."

5 Comments

@Christian Thank you for the simple overview of what needed to be changed.
I have one question with return visit if visit
@christian is this essentially asking: if there is a list called visit, then just return it. otherwise return none?
@TKTK It is using the ternary structure: true_value if boolean_expression else false_value. A list is true if it is non-empty.
Thank you very much!
0

Your indentation is wrong, you return the value of the list after the first occurrence of a person that had signed up and is not a member, you should fix the indentation to:

def guests(member,signup,visitor):
    visit = []
    for person in visitor:
        if person not in member and person not in signup:
            visit.append(person)
    return visit if len(visit) != 0 else None

Also, note how I've changed your return value. You shouldn't return Print('None') because while it will return None eventually (As print returns None, it's not a best practice).

Comments

0

@YashShah and @ThierryLathuille explain in their answers why your code does not work. I would suggest a different approach, using sets

members = ['Alex', 'Danny', 'Kieran', 'Zoe', 'Caroline']
visitors = ['Scott', 'Helen', 'Raj', 'Danny']
signups = ['Helen', 'Scott']


def get_guests(members,signups,visitors):
    return list(set(visitors).difference(set(members).union(set(signups))))
    # alternative:
    # return list(set(visitors) - (set(members)|set(signups)))

print(get_guests(members, signups, visitors))

output

['Raj']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.