These are getting complex, is there an easy way to solve them?

Question

These are getting complex, is there an easy way to solve them?

Answer

It may not necessarily be an easy way, but some exercises that have always helped me and many others are described below, and work for any issue you come across on your programming journeys!

  • The rubber ducky method! You literally talk out loud to a rubber ducky, or anything else, or nothing at all like a crazy coder, and this act of talking out loud lets you hear your mistakes! It often helps to hear them verbally instead of in your head.
  • Good ol’ pen and paper! You’d be surprised how often a problem you’re having can be solved if you write it out step by step. Keeping each step of a problem correctly in your head is a difficult task, and becomes nearly impossible as you progress and do more difficult work.
  • With either method, always break down the problem into its simplest parts.
13 Likes

2 posts were split to a new topic: Need Help Understanding Boolean Expressions

I feel like I am just copying and pasting. What am I supposed to be learning?

28 Likes

I felt the same way at first because i was just copying and pasting lol but for instance this is the way I broke it down after i looked at it better here is an example
and I will break down the first question here

Set bool_one equal to the result of
False or not True and True
You start with not. Not of True is False so your left with False or False and True after that you move to and False and True is False. so you are left with False or False. False or False is False so you have the answer of false

#And Flase or True not not
# not,and,or

#False or not True and True
#False false false

##Question False or not True and True
## You start with not. Not of True is False so your left with False or False and True after that you move to and False and True is False. so you are left with False or False. False or False is False so you have the answer of false
bool_one = False
#False and not True or True
#False false True
bool_two = True
#True and not (False or False
#False False
bool_three =  True
#not not True or False and not True
#True or False and False
bool_four = True
#False or not True and True
bool_five = False


17 Likes

Why is False and True False?

2 Likes

because and requires both conditions to be true. Only if both conditions are met, evaluate to true

this can be very handy for example for a login functionality. Only if the password and username match, log the user in. You don’t want to login the user if only the password or the user name is correct

12 Likes

The good ole pen and paper method works well for me,haha

4 Likes

You may need take a look this logic gates.

4 Likes

I was confused at first but it makes sense now.
I was trying to figure out what is even meant by the question:
Is “False or True” = True? Ummm what?

I found that it helps to replace each False / True with an actual statement, so that you’re evaluating an “and/or” statement to be true or false.
Example:
Right now my TV is actually off. I’ll replace “True” with “TV is off”. I’ll replace “False” with “TV is On”.
The statement “False or True” is now " ‘The TV is ON’ or ‘The TV is Off’ "
As a complete statement, this is obviously a true statement, because it IS “on or off”.
The TV is either on or off? Yeah, it has to be one of those! That’s a true statement.

Now what about “False and True”?
If you said " ‘The TV is ON’ AND ‘The TV is Off’ " that would be obviously False.
It can’t be both on AND off, can it?

28 Likes

Thanks for breaking it down. This made it very simple instead of extreme head scratching!

Hola, necesito ayuda, no e podido ser capas de entender nada :frowning:

Please start a New Topic with important details provided…

  1. A link to the exercise
  2. Your code
  3. Error message(s)
  4. A description of your problem, difficulty with understanding, etc.

This explanation should be at the top - I remember AND OR NOT NAND NOR gates from basic programming hundreds of years ago, but for some reason my brain didn’t activate the information until I read this comment. Thanks!

Edit: I meant myke_s’s comment FYI.

myke_s:
Right now my TV is actually off. I’ll replace “True” with “TV is off”. I’ll replace “False” with “TV is On”.
The statement “False or True” is now " ‘The TV is ON’ or ‘The TV is Off’ "

2 Likes

Can someone explain why bool_four is true

1 Like

Is this the one?

not not True or False and not True

Evaluate the nots first.

not not True =>  True
not True     =>  False

So we now have,

True or False and False

next evaluate the AND

False and False  =>  False

which leaves,

True or False  =>  True
1 Like

Not original poster, but I wanted to know if there’s ever a reason to use not not? It seems like not not always cancels each other? So I’m unsure in what situation we would ever use it.

Also, is there a reason why we have to solve the inner not and then the outer not? LIke couldn’t I just say that not not cancels each other? Because the answer seems the same regardless.

Like for e.g. A not not True is evaluated as:

  1. not not True becomes not False
  2. and then not False becomes True

but is it not correct if I just say off the bat that not not cancels each other? If I see a not not, shouldn’t I just assume it cancels each other so I could safely ignore it from the equation? So if I saw not not True, I would assume the not not is irrelevant, since the answer would still be the original boolean value (True) regardless.

Hope that qn makes sense

One can use it to cast a boolean.

not 1      =>  False

not not 1  =>  True

It’s a convenience in some respects. Using AND will render what it is given, as will OR.

1 and 0  =>  0

1 or 0   =>  1

not and not not have a definitive yield… Always a boolean.

2 Likes

Casting… roughly means to convert something to another type yes?
So what you’re saying is that I can use not not to change certain results into a boolean. Is it safe to assume that it changes any result into a boolean?

Also, thank you for replying to my original post! I replied to this late, but I appreciate the help. Never knew you could do that with not not.

1 Like

Correct. The terms are essentially synonymous. One could call it re-casting since we are changing from one typecast to another.

Correct. It changes any truth value to its boolean equivalent. This can apply to any value or any type.

Consider,

1 and True  =>  True
True and 1  =>  1

We can use the literal to convert 1 to True (its boolean truth value), which would apply to any non-zero number.

0 and True  =>  0
True and 0  =>  0

Wrestling zero to a boolean using the same logic will not work. We can OR it, though,

0 or False  =>  False
False or 0  =>  0

Using logic to recast the numerals is therefore rather arbitrary depending on which case applies. NOT NOT sets the arbitrariness aside…

not not 1  =>  True
not not 0  ->  False

Now we have a single expression that works correctly for both, so now we know we can introduce a variable…

a = 42
not not a  =>  True
b = 0
not not b  =>  False

Of course, Python does have a constructor that can do the same thing…

bool(a)  =>  True
bool(b)  =>  False
2 Likes

This is a great explanation. Tq!

1 Like