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 Python Basics (2015) Logic in Python Conditional Value

Direction 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.

conditions.py
admitted = None
age = 25
if age >= 13:
    admitted = True
    else:
       admitted = False

6 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

You 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
Fable Turas
9,405 Points

It 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
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Fable Turas is correct. If you were coding outside of the challenge it would be completely unnecessary. However, the challenge rules require it.

Thanks, 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
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Normally, 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.

Ahh.. Once again, thanks, Jennifer!

Fable, thanks extremely helpful, thanks!

Fable Turas
Fable Turas
9,405 Points

You 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