Skip to main content
Module 2 · Line Tracking

Lesson 2-7 · Finishing the Sensor Toolkit — Knowledge ChecksAnswer key

Correct answers are marked and the explanation follows each question.

  1. Why can get_left() use reflectance when it was never passed in as a parameter?

    1. A.Python passes the first variable in the file automatically
    2. B.reflectance is a global variable — created outside every function, so every function below it can read it✓
    3. C.get_left() creates its own reflectance each call
    4. D.It cannot; this is a bug

    A name created at the top level of the file is visible inside every function. drivetrain has worked this way since Module 1; reflectance and THRESHOLD work the same way here.

  2. Which condition detects an intersection (both sensors on the line)?

    1. A.left > 0.5 or right > 0.5
    2. B.left > 0.5 and right > 0.5✓
    3. C.left > 0.5
    4. D.not (left > 0.5)

    A cross means BOTH sensors are on black at once, so both comparisons must be true — that's and. or would fire during normal following too.

  3. Both sensors read 0.2 with THRESHOLD = 0.5. What do is_at_cross() and is_either_on_line() return?

    1. A.True and True
    2. B.False and False — neither reading is above the threshold, so neither the and nor the or is satisfied✓
    3. C.False and True
    4. D.True and False

    0.2 > 0.5 is False for both sensors. and needs both True; or needs at least one. Both come out False: the robot is off the line entirely.

  4. Why does get_error() call get_left() instead of reflectance.get_left()?

    1. A.reflectance.get_left() is slower
    2. B.So only one function knows how the hardware is read — if the sensor changes, you fix get_left() and everything else keeps working✓
    3. C.Functions cannot use global variables twice
    4. D.It makes no difference either way

    Functions calling functions keeps each job in one place. get_error() means left minus right; how a reading is taken is get_left()'s business.

  5. Why does set_threshold() need the global keyword when is_at_cross() does not?

    1. A.Because set_threshold takes a parameter
    2. B.is_at_cross() only reads THRESHOLD; set_threshold() assigns to it, and without global the assignment makes a throwaway local variable instead✓
    3. C.Every function that mentions THRESHOLD needs global
    4. D.Because THRESHOLD is in capitals

    Reading a global needs nothing. Assigning inside a function creates a local unless you declare global first — and the real THRESHOLD stays 0.5, with no error to tell you.