MONT 105S, Spring 2009

Home | | Course Schedule | | Assignments | | Lecture Notes

Solutions for Review sheet for final exam

Practice Problems
The following problems are intended to help you study for the exam.

1)

Given the function definition:
	def Mystery( someVal):
		answer = 0
		if  someVal > 2.0:
			answer =  3.0 * someVal
		else:
			answer =  0.0
		return answer
What is the value is assigned to x after execution of the following statement?

x = Mystery(2.5);

Answer: x = 7.5


2)
Consider the following program:

	def Demo( theList, answer):
		theList[0] = theList[0]*2
		answer = theList[0] + 3.5

	myList = [20]
	myResult = 4.8
	Demo(myList, myResult)
	print myList
	print myResult
What is printed out?

Answer: [40] 4.8


3)
a)Write a function,

	def TenToThePower( n ):
that returns 10 raised to the integer power specified by n.
Answer:

def TenToThePower(n):
	answer = 1
	for i in range(n):
		answer = answer * 10
	return answer

b) Write a Python statement to compute 10 to the power of 3 using the above function and store the answer in the variable, tenCubed.

Answer:


4) a) Write a class, named Account, to keep track of a user's bank account. The class should have four properties: name, totalDeposits, totalWithdrawals and totalFunds. The class should have three methods:

Answer:
class Account:
    def __init__(self, userName):
        self.name = userName
        self.totalWithdrawals = 0
        self.totalDeposits = 0
        self.totalFunds = 0

    def makeDeposit(self, amount):
        self.totalDeposits = self.totalDeposits + amount
        self.totalFunds = self.totalFunds + amount

    def makeWithdrawal(self, amount):
        self.totalWithdrawals = self.totalWithdrawals + amount
        self.totalFunds = self.totalFunds - amount
b) Write a main program that makes use of the above class. It should do the following: Answer:
the_name = raw_input("Please enter your name: ")
new_account = Account(the_name)
depositAmt = input("How much do you want to deposit? ")
new_account.makeDeposit(depositAmt)
print new_account.name, "has", new_account.totalFunds, "dollars in the account."

c) What is the constructor function and what is its purpose? When is it called?

The answer is not provided here. Use your notes and textbook to prepare an answer to this question.


5) a) Write the code to create the GUI shown below.

The relative positioning of the elements should be as shown. When the user types their name in the entry field and clicks on the "Say Hello" button, the appropriate message should be typed in the text box as shown. For example, if the user enters "Mary", the message should read "Hello Mary!"

The following are the dimensions of the GUI and elements:

Answer:

from Tkinter import *
def hello_message( ):
    message = "Hello " + entry_field.get( ) + "!"
    myText.delete(0.0, END )
    myText.insert(0.0, message)
    
root = Tk( )
root.title("Exam GUI")
root.geometry("400x200")
app = Frame(root)
app.grid( )

nameLabel = Label(app, text = "Please type your name: ")
nameLabel.grid(row = 0, column = 0, sticky = W )

entry_field = Entry(app, width = 20)
entry_field.grid(row = 0, column = 1, sticky = W )

button1 = Button(app, text = "Say Hello!")
button1["command"] = hello_message
button1.grid(row = 1, column = 1, sticky = E)

myText = Text(app, width = 40, height = 10)
myText.grid(row = 2, column = 0, columnspan = 2)

root.mainloop( )

b) What is meant by "Event driven programming?" What is the purpose of the mainloop in the above program?

The answer is not provided here. Use your notes and the textbook to prepare an answer to this question.


6. a) Write a class, called FallingRock, that inherits from the Sprite class in the livewires games module. The class should have an update function that checks if the object hits the bottom of the screen. If it does, it should reset the position so the rock is at the top of the screen.

Answer:

class FallingRock(games.Sprite):
    def update(self):
        if self.bottom > games.screen.height:
            self.top = 0

b) Write code for the main program that would create an object of type FallingRock. The code should first load in an image named "rock.bmp". It should then create the FallingRock object that has "rock.bmp" as its image. It's initial position should be at the top of the screen and in the middle horizontally. It should be moving directly downward. The code should add this object to the gamescreen.

Answer:

rock_image = games.load_image("rock.bmp")
the_rock = FallingRock(image = rock_image,
                        x = games.screen.width/2,
                        y = 0,
                        dx = 0, dy = 1)
games.screen.add(the_rock)

c) Explain what is meant by class inheritance. Use the example from parts a and b to illustrate your answer.

The answer is not provided here. Use your notes and textbook to prepare an answer to this question.