Mix 'n' Match


#1

I tried this but I'm still getting errors

Here is my code:

# Use boolean expressions as appropriate on the lines below!

#Make me false!
bool_one = (2 <= 7) and "Alpha" == "Bravo" # We did this one for you!

#Make me true!
bool_two = (5 <= 5) not "Alpha" == "Bravo"

#Make me false!
bool_three = (7 == 2) or "Alpha" == "Bravo"

#Make me true!
bool_four = (9 <= 9) not "Alpha" == "Bravo"

#Make me true!
bool_five = (7 <= 7) not "Alpha" == "Bravo"

Here is my error:

Oops, try again. Your code looks a bit off. Check the Hint if you need help! Your code threw the following error: invalid syntax (python, line 7)

(line 7 is bool_two)


10. Mix 'n' Match
#2

https://www.codecademy.com/en/courses/python-beginner-BxUFN/2/6


#3

Here they are:


#4

So clearly we are at liberty to write any expression we wish so long as it includes one of the operators, and we use each one at least once in the exercise. So we can be creative, rather than repetitious. Let the creative juices flow and tax yourself with this one. Be bold.


#5

But what have I done wrong? How am I getting an error? I've used one of these at least once. It says there is something wrong with bool_two, what is wrong with it?


#6

Something is not right here. What error is displayed in red in the console?

Does it contain the following?

File "<stdin>", line 1
    bool_two = (5 <= 5) not "Alpha" == "Bravo"
                                  ^
 SyntaxError: invalid syntax
Unknown error.

Can we write not in the same way we write and and or? Think on it, for a second.


#7

The error says:

Oops, try again. Your code looks a bit off. Check the Hint if you need help! Your code threw the following error:

invalid syntax (python, line 7)

and in the box it says:

  File "python", line 7
    bool_two = (5 <= 5) not "Alpha" == "Bravo"
                                  ^
SyntaxError: invalid syntax

mine is a bit different to your error


#8

Okay, we were able to reproduce the error. I've given you a huge clue. See what you can come up with to solve this error. It will take only one more token.

Consider the following example:

A = True
B = False
print A not B

  File "python", line 3
    A not B
          ^
SyntaxError: invalid syntax
Unknown error.

print A and not B    # True

A = False
B = False
print A or not B     # True

#9

ok well something in there is not supposed to be there because it says invalid syntax so what is this ^ mean in the error thats not how we write not so how do you write not if you don't just write not (in text). we can just write not so that means there has to be some sort of symbol or this ^ is pointing to the invalid syntax, so mine is pointing to this"


#10

Check the example I just edited in the above post.

The error is always before the ^, so work backwards. This error points toward not being incorrectly written. We cannot write not between two expressions without and or or.


#11

Ohhhhhhhhh

got it!!!!

I needed or not instead of just not!

Thanks heaps!!!

(I'm not very good at this and I'm kinda new)


#12

Go back through this entire module and redo every exercise so you get lots of practice and build upon this and the previous lesson. Don't cheat yourself of this very important material and the concepts exposed here. Get it down pat, and it will be in your tool kit forever. You will thank yourself, later. There is no hurry to finish this track. Soak it in.


#13

Will do!
thanks a million!


#14

Supplemental:

Why can we not write not between two expressions? Can you reason this out?


#15

Is it because if you put not it will not do that at all


#16

Sort of. If it is correctly used, it does what it is supposed to do.

Think in terms of relation. An operator generally helps define a relationship between the operands (left and right hand side of the operator are called operands). That is unless of course it is designed to mutate an object.

A and B
A or B

Both of the above describe a relationship between the operands. Can you see not acting in the same way?

Remember, not modifies whatever follows it.


#17

Yep
A not B

means it will do A and not B


#18

But we just learned above that Python will reject this expression. It does not relate A to B so creates a fuzzy situation that the interpreter cannot parse. That's why it stopped parsing where it did in the above problem.

For not to work in Python, it has to either be the first operator in the expression, or be preceded by and or or. When I say it modifies what follows it, I mean that it negates, or toggles the boolean that follows it. True comes back as False, and False comes back as True.


#19

Ok, I kinda get that


#20
not True                => False
not False               => True

To Python, False in numeric terms is 0. All non-zero numbers equate to True.

not 5 and not 0         => False
not 0 and not 5         => False
not 0 or not 5          => True
not 5 or not 0          => True

The above relate to values and their evaluated boolean equivalent. When we say expressions, we mean anything that will yield a value, whereby we evaluate that value to yield a boolean.

not (7 % 4 == 0)        => True
3.14 % 1 and 3.14 - 3   => 0.14000000000000012

Ignore the extraneous floating point error. We can take this to be 0.14.

But shouldn't this be True? Well, 0.14 evaluates to True, so it's still true.

Remember I suggested sticking with this unit until you've given it a full shake? We are just getting started on the things you can learn if you stick with this study for just a day or two to get to know the in's and out's. Up to now we are still only just scratching the surface. Dig deeper, do lots of command line experimentation and burn this stuff into your head. IT IS FUNDAMENTAL to all else in programming.

Exploring just one step further...

   from math import pi
   print pi % 1 and pi - 3            # 0.14159265358979312
   print not not (pi % 1 and pi - 3)  # True
   print bool(pi % 1 and pi - 3)      # True