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's wrong with my code

So I've been working on this program that checks if you're any of my family members, so that I'll take what I've learned into practice. But the problem is, it's long lines of pretty much the same code, but the problem is I've tried to use the operator "or" to check if you're my cousin e.g. elif full_name == "Nikolaj Aagaard" or "Frederikke Aagaard" or "Josefine Bielefeldt" or "Isabella Bielefeldt": print("You're my cousin")

This is taken out of the code, but the strings you see is my cousins and I was just wondering if I could do something like this so my code will be shorter and easier to understand.

Here's the whole program if you need more information where this is taken from, it's not done yet the program,

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

name = input("What is your name? ") last_name = input("What is your last name, {}? ".format(name)) full_name = name + " " + last_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 full_name == "Mikkel Bielefeldt": print("Hey! How funny we're the same person") elif full_name == "Lene Bielefeldt": print("You're my mom!")

Checks to see if it's my mom, dad or sister

elif full_name == "Morten Bielefeldt": print("You're my dad!") elif full_name == "Lærke Bielefeldt": print("You're my sister!")

Checks to see if it's either of my grandparents

elif full_name == "Jette Bielefeldt": print("You're my grandmother") elif full_name == "Søren Bielefeldt": print("You're my grandfather") elif full_name == "Else Aagaard": print("You're my grandmother")
elif full_name == "Søren Aagaard": print("You're my grandfather")

Checks to see if it's my cousins

elif full_name == "Nikolaj Aagaard" or "Frederikke Aagaard" or "Josefine Bielefeldt" or "Isabella Bielefeldt": print("You're my cousin")

elif full_name in family: print("You're part of my family {}!".format(name)) else: print("Maybe if you ask {}, you can be part of my family.".format(full_name))

2 Answers

Oszkár Fehér
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Oszkár Fehér
Treehouse Project Reviewer

Hi Mikkel Bielefeldt. If you are using the list of names

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"]

then you can do something like this for example: Instead of this line below

if full_name == "Mikkel Bielefeldt":

you can use just the list

if full_name == family[0]:   --> 0 from the first element in the list, in this case the first name from the list

or

elif full_name == family[1]:

and the same with the rest of the family if you know the position of each name in the family list In case of multiple names

elif full_name == "Nikolaj Aagaard" or "Frederikke Aagaard" or "Josefine Bielefeldt" or "Isabella Bielefeldt":
    print("You're my cousin")

then make sure that all those names are one after another in the family list as the following example, the cousins' name will be at the end of the list, not necessarily at the end, it can be anywhere in the list just make sure that all those names are one after another in the list, like below

family = [<the rest of the name here(total name 10 with index of 9)>, "Nikolaj Aagaard", "Frederikke Aagaard", "Josefine Bielefeldt", "Isabella Bielefeldt"]

then you can use something like this

elif full_name in family[<here the index from which the cousins name starts>:]:   --> using a slice from the list
elif full_name in family[9:]:    --> this will check all the names which come after the name with index 9 till the end of the list

Also, you can use capitalize() on the input to make sure the name entered it's capitalized as in the family name list. I ran a few times your code, it's a nice little program, a great way to exercise list, indexes, slices, keep up like this! I observed some weird characters in the name list like, æ, ø, å make sure that these are handled correctly unless you have your keyboard layout for these letters. I hope this will help you out. Happy coding!

Oszkár Fehér
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Oszkár Fehér
Treehouse Project Reviewer

Sorry, I just observed that the last 2 conditions should have in instead of ==, I corrected in the above answer.