Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


Mikkel Bielefeldt
Mikkel Bielefeldt
3,227 Points

What is wrong with my code?

I have been through Python Basics and Python Collection, and I thought maybe I should try to take what I've learned and practice it, but there seems to be a problem with my code in the "if name + last_name = family" line of code. What I'm trying to do is to, get the persons name and last name to see if they're part of my family and in the list family is all of my family members, but I don't know how to write code that'll check if they're part of the family list. I've tried to use "==" instead of "=" but that doesn't seem to work either.

This program is testing if you're part of my family

name = input("What is your name? ") last_name = input("What is your lastname, {}? ".format(name))

family = ["Mikkel Bielefeldt", "Lene Bielefeldt", "Lærke Bielefeldt", "Morten Bielefeldt", "Jette Bielefeldt", "Søren Bielefeldt", "Rikke Bielefeldt", "Jacob Risgaard", "Josefine Bielefeldt", "Isabella Bielefeldt", "Anne Bielefeldt", "Agnes Bielefeldt", "Janne Aagaard", "John Kirkegård", "Frederikke Aagaard", "Marie-Louise Aagaard", "Poul Aagaard", "Nikolaj Aagaard", "Else Aagaard", "Søren Aagaard"]

if name + last_name = family

2 Answers

Steven Parker
Steven Parker
220,352 Points

You're on the right track, a single "=" is an assignment operator and clearly what you might use in a test is an equality comparison ("==") instead.

But a simple comparison is not enough to find an item within a list. Luckily Python has a membership operator ("in") that will do what you want. Also, don't forget that you need a space between the names so the combined string can match:

if name + " " + last_name in family:
    # It matches