10. Mix 'n' match


#1



I get an error when i used this line on Bool_three on exercise 10 Mix 'n' Match.
Am i missing something here or is the program that is correcting what i write bugged, because this is the third exercise on Conditionals & Control Flow that says im doing wrong when im actually doing right...


True and False or not True == False


Mix and Match What variables did I miss
#2

@varriot: What's the error?
The statement you've posted is correct, nothing wrong with it.

Are you using the conditional structure or something?


#3

It wants bool_three to be False. The statement you wrote is True. Remember the order of operations: not, and, or.

True and False or not True == False --> not True = False

True and False or False == False --> True and False = False

False or False == False --> False or False = False

False == False --> False == False = True

Replied to wrong person sorry


#4

Im getting the "Oops, try again. Double check your value for bool_three!"

This was how i wrote the entire section

Make me true!

bool_two = not True or True == True

Make me false!

bool_three = True and False or not True == False

Make me true!

bool_four = not 100 == 20 * 6 and True == True

Make me true!

bool_five = 1000 / 1000 == 1 and not 44 == 2 == True

I had written some equations on bool_three aswell but just to make sure i hadnt done the math wrong i boiled it down to just True and False. Still wouldnt compute.


#5

@varriot:

bool_three = True and False or not True == False

That will bring back True since when you have OR, you only need one of the parts to be True so the logical value from that condition will be True as well.
Try something else such as removing that not from not True == False and it should work.


#6

But then im a bit lost, because as i understood it, OR is interpreted last and the not first.
Meaning the not True is processed first into False
Then the and is processed; True and False becomes False
And lastly the False or False becomes False

Ill try to highlight how i understand it in my head.
True and False or not True
True and False or False
False or False
False

I understand i could try something different, but i feel like i need to learn why it doesnt work here as much as i need to learn when it works, sorry if its hard to interpret what im writing.


#7

Like it says in the instructions:
Boolean operators aren't just evaluated from left to right. Just like with arithmetic operators, there's an order of operations for boolean operators:

not is evaluated first;
and is evaluated next;
or is evaluated last.


#8

You've got it right, but after all of that you have False == False, which is True. However, the exercise wants bool_three to be False.


#9

@varriot: Okay, so let's go part by part of the expression.

True and False or not True == False
True and False or False == False
False or False == False
False or True
True

How it looks:

The comparison has a higher precedence comparing to OR, AND and NOT source


#10

But then it just might be a lack of knowledge on my part, because i though == is just equal. As in the value on both sides match each other, i just used it as a substitute for is. I didnt realise it alters the result. What exactly does the == mean then?


#11

@varriot:

a = 2
b = 3
a == b # comparing elements
a = b # assigning the value stored in the variable "b" to the variable "a"

Edit: I left a link in my last post with a list about that topic!


#12

Ok, now i think i atleast understand the part i did wrong from my perspective.
My chain of thought was that this whole line True and False or not True was False which i tried to show by using the == as a sign that the value on both sides of it were the same. While infact the == False was completely redundant to the assignment as it altered the line further beyond what i intended.
I removed the == False at the end and then it computed th way i had intended.


#13

@varriot: I'm glad you understood and passed !! :sunny:


#14

Just to hash it out further, the == at the end, what made it so that the value changed? Seeing as it seemed to change the value to its opposite. I mean, i feel like i dont entirely understand the function of the == anymore, i thought it was just simply a = but as that is used in variables they needed a substitute.


#15

@varriot: The precedence of the operators was evaluated, each one at a time.
Since all we had left were OR and ==, the program knows that the precedence of == is higher than OR.
Whenever the code encounters such thing, it will ask "is something equal to another thing?".
If they're the same, it will evaluate as True, if not, it will evaluate as False.


#16

Ok, so to dumb it down even further, it interpreted it as two negatives becomes a positive.


#17

@varriot: Yes! It will ask "is False equal to False?"
Until this day, False is equal to False, just ike True is equal to True, so it will evaluate that expression, False == False, are True.


#18

I think that im getting to hung up on the words them selves, that is why its a bit of a learning curve getting into the right frame of mind. But thanks, you have been immensly helpful!


#19

@varriot: You'll get used to it quickly, trust me!
Glad to help :sunny:


#20

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.