1

So I'm learning programming with Karel and I'm a little confused about conditionals with parenthesis and specifically what would happen in a loop.

def build_line():
    while not (front_is_blocked() or beeper_is_present()):
        face_east()
        put_beeper()
        move()
def build_line():
    while not front_is_blocked() or not beeper_is_present():
        face_east()
        put_beeper()
        move()

In the line while not (front_is_blocked() or beeper_is_present()) and while not front_is_blocked() or not beeper_is_present() do they mean the same thing? That the loop will only start if the front is clear OR there are no beepers present?

hsuehtt98
  • 13
  • 2

3 Answers3

0

No. The expression not (front_is_blocked() or beeper_is_present()) is equivalent to not front_is_blocked() and not beeper_is_present() per DeMorgan's Laws. Notice the and vs or in your 2nd code sample.

Allan Wind
  • 23,068
  • 5
  • 28
  • 38
  • how about `while not (front_is_blocked() and beeper_is_present())`? would it become `not front_is_blocked() and not beeper_is_present()`? – hsuehtt98 Oct 15 '22 at 06:08
  • No. `not (front_is_blocked() and beeper_is_present())` is equivalent to `not front_is_blocked() or not beeper_is_preesent()` – Allan Wind Oct 15 '22 at 06:09
  • oh so the not operator not only inverses the function values but also the connective? – hsuehtt98 Oct 15 '22 at 06:13
  • Yes. Check out the link to DeMorgan's Laws. I use it often to simplify expressions. Make sure you accept the best answer so we know you are all set. – Allan Wind Oct 15 '22 at 06:14
  • is safe to assume that `while not (front_is_blocked() and beeper_is_present())` is therefore the same as `while not front_is_blocked() or not beeper_is_present()` then? – hsuehtt98 Oct 15 '22 at 06:17
  • Yes, they are the same. – Allan Wind Oct 15 '22 at 06:30
0

They aren't the same. Here's a truth table where each column represents a different combination of front_blocked and beeper_present values:

T T F F  front_blocked
T F T F  beeper_present
-----------------------
T T T F  front_blocked or beeper_present
F F F T  not (front_blocked or beeper_present)
F T T T  not front_blocked or not beeper_present

Note that front_blocked or beeper_present is a simple or, which means it's true as long as there's at least one T value (i.e. in all scenarios except F F), and the following line not (front_blocked or beeper_present) is the inverse of that, making it true only for F F (this is a logical NOR).

not front_blocked or not beeper_present inverts the individual values before taking the or, so it's true as long as there's at least one F value, making it true in all scenarios except T T (this is a logical NAND -- you could also write it as not (front_blocked and beeper_present)).

Samwise
  • 68,105
  • 3
  • 30
  • 44
0

Both the functions are not same. In first function , while loop will only execute when both other functions (ie front_is_blocked and beeper_is_present) returning False, while in later function even if either of function is returning True, while statement will execute.

codeman
  • 1
  • 1