Quiz 2a#

Question 1#

What typically calls an event handler?

Some code that you didn’t write which generates the event.
The code you write. \


Some code that you didn’t write which generates the event. \


Question 2#

In CodeSkulptor, how many event handlers can be running at the same time ?

0
1
Unlimited, i.e., 0 or more \


1 \


Question 3#

What are the three parts of a frame?

Refer to the video on SimpleGUI.

Border
Control Area
Title
Background Area
Status Area
Options Area
Keyboard
Canvas
Mouse \


Control Area
Canvas
Status Area \


Question 4#

For the SimpleGUI-based programs in this course, we recommended breaking down an interactive Python program into seven parts. Below, these parts are listed alphabetically.

  1. Create frame

  2. Define classes

  3. Define event handlers

  4. Initialize global variables

  5. Define helper functions

  6. Register event handlers

  7. Start frame and timers

However, in lecture, we recommended a particular ordering of these parts.

Enter 7 numbers in the range 1–7, separated only by spaces, to indicate the recommended ordering of the preceding elements of an interactive Python program. For example, if you think that the first action in your program should be to register your event handlers, enter 6 as the first number in the sequence.

Enter answer here:\


4 5 2 3 1 6 7


Question 5#

Assume the following global definition is part of your program.

x = 5

If each of the following function definitions are also part of your program, which of them needs a global x declaration? You can try each definition in CodeSkulptor .

  def c(y):
    return x + y

\

  def d(y):
    y = x + y
    return y

\

  def b(x,y):
    x = x + y
    return x

\

  def a(y):
    x = x + y
    return y

\


def a(y):
    x = x + y
    return y
  • c(y) only needs to read x, so global x not needed

  • d(y) ditto

  • b(x, y) has x as an argument, so the function is meant to get x through its argument list and the programmer does not need access to x through global x

  • In a(y), x = x + y won’t make any sense without global x, because the function only returns y.


Question 6#

Consider the following code.

count = 0

def square(x):
    global count
    count += 1
    return x**2

print square(square(square(square(3))))

What is the value of count at the end? Enter a number. (You can double check your answer in CodeSkulptor if you wish.)

Enter answer here:\


43046721

  • square and print work on the returned values. count does not play a role on the line with print and square


Question 7#

Consider the following code.

a = 3
b = 6

def f(a):
    c = a + b
    return c

Which names occur in the global scope?

f
b
c
a\


a, b


Question 8#

Consider the following code.

a = 3
b = 6

def f(a):
    c = a + b
    return c

Which names occur in a local scope?

f
b
a
c\


  • c

  • the difference to previous question is local


Question 9#

Which of the following are valid calls to create_frame?

Look at the documentation for SimpleGUI frames, but also try the code in CodeSkulptor.

  frame = simplegui.create_frame(100, 100, 100)

\

  frame = simplegui.create_frame("My Frame", 200, 200, 200, 200)

\

  frame = simplegui.create_frame("Testing", 200, 200, 300)

\

  f = simplegui.create_frame("My Frame", 100, 100)

\


frame = simplegui.create_frame(“Testing”, 200, 200, 300)
f = simplegui.create_frame(“My Frame”, 100, 100) \


Question 10#

Which of the following are valid ways of making a canvas with a red background?

Look at the documentation for SimpleGUI constants, but also try the code in CodeSkulptor.

  import simplegui

frame = simplegui.create_frame("My Frame", 100, 100)
frame.set_canvas_background("#FF0000")
frame.start()

\

  import simplegui

frame = simplegui.create_frame("My Frame", 100, 100, "Red")
frame.start()

\

  import simplegui

frame = simplegui.create_frame("My Frame", 100, 100)
frame.set_canvas_background("Red")
frame.start()

\

  import simplegui

frame = simplegui.create_frame("My Frame", 100, 100)
frame.set_canvas_background(Red)
frame.start()

\


import simplegui

frame = simplegui.create_frame("My Frame", 100, 100)
frame.set_canvas_background("#FF0000")
frame.start()
import simplegui

frame = simplegui.create_frame("My Frame", 100, 100)
frame.set_canvas_background("Red")
frame.start()