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

What's wrong with my code?

So I'm working on this program that checks to see if you're part of my family, and then just to get more practice in, I did so that you need to answer some questions if you wanna be part of my family. But I don't know if what I wrote was wrong because I'm getting a Syntax Error, because apparently I don't know how to use if, elif and else. It's the last else in my program that's the problem.

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

name = input("What is your name? ") middle_name = input("What is your middle name, {}? If you don't have one {}, just hit enter! ".format(name, name)) last_name = input("What is your last name, {}? ".format(name)) if len(middle_name) < 1: middle_name = " " elif len(middle_name) > 0: middle_name = " " + middle_name + " "

full_name = name + middle_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 Bach", "Poul Aagaard", "Nikolaj Aagaard Balsen Nielsen", "Else Aagaard", "Søren Aagaard"]

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

if full_name == "Mikkel Bielefeldt": print("Hey! How funny we're the same person") elif full_name == "Lene Bielefeldt": print("You're my mom!") 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 Balsen Nielsen": print("You're my cousin") elif full_name == "Frederikke Aagaard": print("You're my cousin") elif full_name == "Isabella Bielefeldt": print("You're my cousin") elif full_name == "Josefine Bielefeldt": print("You're my cousin")

Checks to see if you're my aunts or uncles

elif full_name == "Rikke Bielefeldt": print("You're my aunt") elif full_name == "Anne Bielefeldt": print("You're my aunt") elif full_name == "Janne Aagaard": print("You're my aunt") elif full_name == "Marie-Louise Aagaard Bach": print("You're my aunt")

elif full_name == "Jacob Risgaard": print("You're my uncle") elif full_name == "John Kirkegård": print("You're my uncle") elif full_name == "Poul Aagaard": print("You're my uncle")

Checks to see if you're my great grandma

elif full_name == "Agnes Bielefeldt": print("You're my great grandmother")

questions = input("What is your full name? \nHow old are you? \nAre you a female or male? \n What is your phone number?")

else: print("I'm sorry, but you can fill out some information, and we'll what we can do.".format(full_name))

information = input("Do you wanna fill out this information? Y/N: ") if information.lower() = "n": print("I'm sorry to hear that.") break elif information.lower() = "y": print("Alright then! Just fill out the questions below!") print(questions)

You should check out the markdown cheatsheet and use that to post your code so we can help you better.

Steven Parker
Steven Parker
243,356 Points

Python is impossible to test when it is unformatted. As I suggested in your previous question, you can find the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

1 Answer

Steven Parker
Steven Parker
243,356 Points

I notice there's a "break" near the last "elif". With the code unformatted this is just a wild guess, but since "break" is only allowed within a loop that could be causing a syntax error if that bit of code is not part of a loop.

Please add formatting to the code above so we can confirm if that's the issue, and be sure to use it in any future questions!

Another (and even better) way to share code is to make a snapshot of your workspace and post (only) the link to it here.