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 trialMohammad Zryab
4,109 PointsDirection on if statements in Python
I'm trying to write an if statement as follows: If 'age' is equal to or greater than 13, then admitted is equal to "True", otherwise (else) admitted is equal to "False".
I'm trying this in one of the code challenges but I'm not sure where my misstep is here.
admitted = None
age = 25
if age >= 13:
admitted = True
else:
admitted = False
6 Answers
Jennifer Nordell
Treehouse TeacherYou have two small errors here and one isn't really an error. They're going to send in data to test that your code works. So you're overwriting what they're sending by resetting the age variable to the age of your choosing. So you need to remove the 'age = 25' line. Secondly, you have an indentation error. Your else should line up with your if which means your two admitted should line up. Take a look here:
admitted = None
if age >= 13:
admitted = True
else:
admitted = False
Happy coding!
Fable Turas
9,405 PointsIt should actually also be noted that your entire 'else' block is not necessary. Since 'admitted' is set to None it starts out with a falsey value, so you only need to change the value to True when 'age' passes the set condition. If 'age' fails the condition testing, 'admitted' would be unchanged, retaining its value of None which would evaluate to False for any conditional test run against it.
Jennifer Nordell
Treehouse TeacherFable Turas is correct. If you were coding outside of the challenge it would be completely unnecessary. However, the challenge rules require it.
Mohammad Zryab
4,109 PointsThanks, Jennifer!
One quick follow-up question on my end would be: shouldn't there be a variable called age? Because if there's no variable called age, how does Python know what to test against?
Jennifer Nordell
Treehouse TeacherNormally, you'd be right except for this line inside the instructions: "I'm going to create a variable named age." What they mean is that they're creating a variable outside of your code at Treehouse. Then Treehouse is going to send in probably several different ages to make sure that your code passes in every case. So you were writing over whatever they were sending.
Mohammad Zryab
4,109 PointsAhh.. Once again, thanks, Jennifer!
Jennifer Nordell
Treehouse TeacherYou're quite welcome! :)
Mohammad Zryab
4,109 PointsFable, thanks extremely helpful, thanks!
Fable Turas
9,405 PointsYou are right Jennifer Nordell; my bad. I was only looking at the first task of the challenge and the code above. Now I feel like I should write Kenneth about encouraging redundant coding techniques in his challenges. LOL