Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

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
229,608 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