Week 2 - Event-driven programming, local/global variables discussion#

Goals#

  • Learn the basics of event-driven programming

  • Understand difference between local and global variables

  • Create an interactive program that plays a simple guessing game

Week 2a - Interactive Applications in Python#

Event-Driven Programming#

  1. What characterizes an event-driven program?

  1. When would you use event-driven programming?

  1. What happens with the event queue if you press a button and if a timer times out?

  1. How do you describe the reaction to a key press (an event in general)?

  1. What does the Hollywood saying “Don’t call us, we’ll call you” mean in context of event-driven programming?

Local vs. Global Variables#

  1. Imagine you have created a variable outside of a function. How can you modify this variable (without providing the variable as a parameter to your function)?

  1. When should we use local variables and when global?

  1. What are the advantages/perils of using global variables?

  1. Why does this course introduce global variables if we should better not use them?

SimpleGUI#

  1. What is simplegui?

  1. What are frame, control area, canvas and status area?

  1. a. What is the program structure advocated by Scott Rixner for interactive programs using Codeskulptor? a. What is the advantage of this structure?

Week 2b - Buttons and Input Fields#

Buttons#

  1. In Joe Warren’s calculator: What are the concepts (1) store and (2) operand for?

  1. Which object do we have to create before we can create a button?

  1. Joe Warren implements first the global variables, output() and swap() functions. To implement these, he did not use simplegui. Why?

Input Fields#

  1. The buttons and text inputs are created as follows:

    def button_handler():
     	pass  # your code
    
    def input_handler(text):
     	pass  # your code
    
    # ...
    f.add_button('label', button_handler, 50)
    f.add_input('label', input_handler, 50)
    

    Why does the interface of input_handler() differ from button_handler()?

Visualizing Events#

Programming Tips - 2#

  1. How can we shorten the following statement:

    if x == False or y == True:
        z = 5
    
  1. Why should we adhere to PEP 8?

Mini-project #2 - “Guess the Number!”#

Practice Mini-project: Magical Octosphere Reloaded#

  1. Compared to the practice project of the second week, we just have to add three lines of code. Why is that so simple?

“Guess the Number!”#

  1. What is binary search algorithm?

  1. How is binary search related to the game?