Lesson 1-8 · Hello, Python — Knowledge ChecksAnswer key
What's the main advantage of a general-purpose language like Python over Blockly?
Blockly gave you the ideas, and it can do loops and functions perfectly well. What it can't do is go beyond the blocks provided, or reach the huge collection of ready-made Python libraries you can build on as programs get more complex.
A program starts with "from XRPLib.rangefinder import Rangefinder" and then calls rangefinder.distance(). It fails with a NameError. What was skipped?
Importing the class makes Rangefinder available; it does not create rangefinder. The middle step — rangefinder = Rangefinder.get_default_rangefinder() — is what gives that name an object to stand for the hardware.
Where do the three steps actually go in a real program?
Setup happens once: one import, one instance. Using the device is the actual program — it can be twenty lines later, inside a loop, or inside a function, and it happens as many times as you need. The three-line examples are squashed together only to show the shape.
Which line is written correctly?
Lowercase print, parentheses around the call, and quotes around the text. The others break one rule each.
What do the parentheses in board.wait_for_button() do?
Parentheses are what turns a name into a call. wait_for_button needs no arguments, so they're empty — but leave them off and Python just names the function without running it.
Why does drivetrain.straight(-30) drive the robot backward?
A negative distance means backward, exactly like typing a negative cm value into the block.
Find the bug in the code above.
Python is case-sensitive. Print with a capital P isn't the same as the built-in print.