Practice Exercises for Interactive Applications#

1#

Given the program template below, write a Python function print_goodbye() that defines a local variable message whose value is "Goodbye" and prints the value of this local variable to the console. Note that the existing global variable message retains its original value "Hello" after the call to print_goodbye() completes. Print goodbye templatePrint goodbye solutionPrint goodbye (Checker)

# Printing "Goodbye" with a local message variable

##################################################
# Student should enter function on the next lines.

def print_goodbye():
    message = 'Goodbye'
    print(message)

##################################################
# Tests

message = "Hello"
print(message)
print_goodbye()
print(message)

message = "Ciao"
print(message)
print_goodbye()
print(message)


##################################################
# Output

#Hello
#Goodbye
#Hello
#Ciao
#Goodbye
#Ciao

2#

Given the program template below, write a Python function set_goodbye() that updates a global variable message with the value "Goodbye" and prints the value of this global variable to the console. Note that the existing global variable message has its original value "Hello" modified to "Goodbye" during the call to set_goodbye(). Set goodbye templateSet goodbye solutionSet goodbye (Checker)

# Printing "Goodbye" with a global message variable

##################################################
# Student should enter function on the next lines.

def set_goodbye():
    global message
    message = 'Goodbye'
    print(message)


##################################################
# Tests

message = "Hello"
print(message)
set_goodbye()
print(message)

message = "Ciao"
print(message)
set_goodbye()
print(message)


##################################################
# Output

#Hello
#Goodbye
#Goodbye
#Ciao
#Goodbye
#Goodbye

3#

Challenge: Given the program template below, implement four functions that manipulate a global variable count as follows. The function reset() sets the value of count to be zero, the function increment() adds one to count, the function decrement() subtracts one from count, and the function print_count() that prints the value of count to the console. Count operations templateCount operations solutionCount operations (Checker)

# Functions to manipulate global variable count

##################################################
# Student should enter function on the next lines.
# Reset global count to zero.
# Increment global count.
# Decrement global count.
# Print global count.

# global variables
count = 0  # not strictly needed but good for readability


def reset():
    global count
    count = 0


def increment():
    global count
    count += 1


def decrement():
    global count
    count -= 1


def print_count():
    print(count)

##################################################
# Test

# note that the GLOBAL count is defined inside a function
reset()		
increment()
print_count()
increment()
print_count()
reset()
decrement()
decrement()
print_count()

###################################################
# Output
#1
#2
#-2
  • Note that we do not need to declare a variable as global if we do not have to modify it but only read.

4#

Complete the program template below so that the resulting CodeSkulptor program opens a frame of size 100×200 with the title "My first frame". You will need to add only two extra lines of code. Two extra lines templateTwo extra lines solutionTwo extra lines (Checker)

# Open a frame

##################################################
# Open frame
# Student should add code where relevant to the following.
try:
    import simplegui
except ImportError:
    import SimpleGUICS2Pygame.simpleguics2pygame as simplegui


message = "My first frame!"

# Handler for mouse click
def click():
    print(message)

# Create a frame and assign callbacks to event handlers
frame = simplegui.create_frame("My first frame", 100, 200)
frame.add_button("Click me", click)

frame.start()
  • if you forget frame.start() the GUI won’t respond.

5#

Given the program template below, modify the program to create a CodeSkulptor frame that opens a 200×100 pixel frame with the title "My second frame". Remember to use the Docs to determine the correct syntax for the necessary SimpleGUI calls. Open frame templateOpen frame solutionOpen frame (Checker)

# Open a frame

##################################################
# Open frame
# Student should add code where relevant to the following.

try:
    import simplegui
except ImportError:
    import SimpleGUICS2Pygame.simpleguics2pygame as simplegui

message = "My second frame!"

# Handler for mouse click
def click():
    print(message)

# create a frame
frame = simplegui.create_frame(message, 200, 100)

# Assign callbacks to event handlers
frame.add_button("Click me", click)

# Start the frame animation
frame.start()