Why didn't my boolean work?

Question

I wrote my code for the boolean and when I press Run it isn’t accepted; why is this?

Answer

Python, and all programming languages, require you to match their syntax exactly. Note that there are no quotation marks around a boolean value, and that they are capitalized. In other languages you may see booleans written as true and false, but in Python they must be True and False. Keep that in mind for future exercises where you’ll be asked to check if something is True or False so you don’t compare to a string, like ”True”!

Take a look at the code below for a better understanding:

bool_1 = True
bool_2 = False
bool_3 = true     # invalid
bool_4 = false    # invalid
bool_5 = "true"   # invalid
bool_6 = "false"  # invalid
9 Likes

I just started learning python today so not exactly an expert, but what I noticed is that this program is somewhat strict. you have to write True or False, the capital letters seem important for the first letters. So you cannot write tRue, trUe, truE, TruE and so on…

12 Likes

i used lower case for false and it ran fine but only accepted capitalised true ???

If True corresponds with number 1 why is name_is_maria = 1 incorrect; for age_is_12 I typed age_is_12 = 0 which worked.

9 Likes

I have the same question, can someone advice please?

1 Like

i have the same question!
can someone help me get the answer?

1 Like

Entering name_is_maria = 1 assigns the value one to your variable called name_is_maria, just as if you used = 2, =10, = 15.5, or any other value. Instead, you want to assign it a value of True or False.
After assigning it a value of True, I then entered name_is_maria += 3 to add three to the stored value of True and 1. The print function then returned the value 4 for me, which demonstrates that the name could be True and have the integer value of 1.

8 Likes

Thanks for your advice!! Really helped me.

I used 0 for False in the first line and it accepted it, but when I tried to used 1 for True in the second line it said invalid. I just wanted to see if it would take 1 and 0 because it said 1 and 0 were equivalent to True and False

Why did it work for False but not for True?

2 Likes

Same exact scenario, can someone go into detail on this? was it a mistake?

2 Likes

Yes, but you STILL didn’t answer the question. We ALL KNOW we aren’t looking to assign “true” or “false” to the INTEGER.

What everyone wants to know, is why, when the lesson DEFINED 1 = TRUE when assigning boolean values (as in assigning the integer “1” to a variable would translate to TRUE) just like the lesson DEFINED 0 = FALSE.

When defining “age_is_12”, using “age_is_12 = 0” WORKED in every case. The program accepted that we are trying to communicate the fact that Maria’s stated age isn’t 12. This would make sense for one to assume that if THAT worked, then the SAME LOGIC would apply the variable “name_is_maria = 1” as in, "YES her name IS maria. 1 = TRUE". We are assigning a boolean value of TRUE; AKA 1". However, the lesson did NOT progress, and instead completely contradicts itself, which understandably would be confusing to anyone who understands basic logic and trusts the lesson being presented as absolute truth.

Of course no one is trying to say that “the number one is a truth”, that is absurd and totally out of context.

5 Likes

so I have
age_is_12 = false
then it just will say “false is not defined” how do I define true and false?

They are built-in so there’s no need to define them but you need to captialise them when written out like this-
a = True or a = False

2 Likes

Pythons syntax is really strict so when making boolean values you have to make it with a capital T or F.
Shoe1 = True
Shoe2 = False

If attempting to use 1 and 0 to define True and False you must define them as a boolean < bool() > otherwise Python will assume they’re integers. I’ve also noted that any whole number can be used instead of 1 and it will remain True. I’m assuming this is because Python hasn’t been told otherwise and hence works on the true/false principle of you either have something or nothing.

I’m a beginner so I don’t know why you’d use 1 and 0 over True and False, but since so many people asked. This is the same format/pattern used for converting from one data type to another; float(), int(), str(), etc.

2 Likes

There’s a short but helpful summary at: https://docs.python.org/3/library/stdtypes.html#truth-value-testing. So it’s intended behaviour rather than something overlooked (only 0 or the equivalents in similar numeric types would be False). The result of using an object like a boolean is commonly referred to as the “truthiness” of an object (there are some guides around if you wanted to look into more about this).

So calling bool is perfectly valid but you may often see things like-

if my_list:
    do something...

which relies on the fact a list has a truthiness of False if empty and True otherwise (regardless of the actual contents).

Ah, I probably should have replied to the comments asking the question but it was 9 months ago and it’s covered in the next few lessons (assuming they’re following the python course) so they should know by now. However, since this thread is linked at the bottom of the exercise I decided to answer the question for any other newbies who might drop by.

Thank you for the further reading though! I understand calling bool in the situations you gave, which confirms the something=true, nothing=false premise (which I probably just worded badly, I don’t see it as an oversight). My question/comment was more why people are trying to call bool for hard-code 1 and 0 when they could just write True and False. I suppose it’s useful for experimentation to see how it works but otherwise, it’s just funny to me. A case of thinking they can make their work easier and instead making it harder I guess.

1 Like

Ah, might’ve misunderstood your query. Hard coding it does seem like a mistake, or at least not best practices.

The True & False are always supposed to be capitalized in coding.

I had the same issue and to solve it I had to change the f to F and leave out the space between the = and the F