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
Mikkel Bielefeldt
3,227 PointsWhat's wrong with my code?
My code gives me a red line, and I think it has something to do with the if, elif and else statements before it, but I don't really, cause it doesn't give me that in Python IDLE. I'll post the whole program because I think it has something to do with all the elif statements I use. I don't know how to quote that exact line of but it's the last portions of code in my program.
import sys
# 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")
else:
print("I'm sorry, but you can fill out some information, and we'll see what we can do.".format(full_name))
information = input("Do you wanna fill out this information? Y/N: ")
if information.lower() == 'n':
print("Alright, bye")
sys.exit
elif information.lower() == 'y':
print("Alright then! Just fill out the questions below.")
full_name2 = input("What is your full name? ")
age = input("How old are you? ")
gender = input("What is your gender? ")
phone_number = input("What is your phone number? ")
print(full_name2 + " " + age + " " + gender + " " + phone_number)
2 Answers
Steven Parker
243,656 PointsThis code looks OK at first glance, and I tried running it and got no problems.
Could you be more specific about the issue you are having? I'm not sure what "gives me a red line" means.
Update: I ran this through PEP8 Online and it just complained about the use of 2-space indents, a few extra long lines, and some trailing white space. I think most Python interpreters will tolerate all of these and give no runtime errors. The workspace was happy with it.
You could use that resource yourself anytime you want to check your code for PEP8 compliance (and to help fix it so that it is).
Jonathan Grieve
Treehouse Moderator 91,254 PointsI suspect "red line" refers to Syntax Highlighting. And while I haven't run this particular script through my own text editor, assuming it does compile I assume it refers to Pythons rules on coding standards. e.g. tab indexes of 4 spaces etc. See if that's what your editor is complaining about. :-)
Mikkel Bielefeldt
3,227 PointsSo the "information" variable gets highlighted with red text when I try to run it in Workspaces, but it doesn't have highlighted red text when I run it in Python IDLE
Steven Parker
243,656 PointsIt was a workspace I tried it in, I did not see any special coloring on "information".
Mikkel Bielefeldt
3,227 PointsOkay. I guess I'll just try messing around with it a little, and if it doesn't work I'll just skip it.