Search code examples
pythonkarel

Python conditions with parenthesis in a while not loop


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?


Solution

  • 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.