Which keyword defines a function in Python?
- A.function
- B.def
- C.define
- D.func
Answer Why?
Given def square(side_length):, what does calling square(45) make the robot do?
- A.Draw a square with 45 cm sides
- B.Draw a square with 90 cm sides
- C.Turn 45 degrees
- D.Cause an error — you must pass two numbers
Answer Why?
A variable created inside a function (like angle above) can be used…
- A.Anywhere in the program
- B.Only inside that function — it is local
- C.Only after the program ends
- D.Only in other functions
Answer Why?
If a function's variables are local, how can square() use drivetrain, which was created in the main program?
- A.Because drivetrain is special to XRPLib
- B.When a name is not local, Python looks outside the function for it — functions can read names from the surrounding program
- C.It does not work; that program has a bug
- D.Because drivetrain is passed in automatically
Answer Why?
What's wrong with this? def multiply(x, y)
- A.multiply should be capitalized
- B.It is missing the colon: def multiply(x, y):
- C.x and y need quotes
- D.Nothing is wrong
Answer Why?
Given this definition, what does calling polygon(6) draw?
def polygon(sides, side_length=30):
- A.A hexagon with 30 cm sides
- B.A hexagon with 6 cm sides
- C.A 30-sided shape
- D.An error — polygon needs two arguments
Answer Why?