16 Solutions

Chapter 5 – Variables

QUESTIONS

(a) To store data and information.
(b) Examples of variables types include: integers, floats, strings, booleans.
(c) An element.
(d) Nesting.

EXERCISES

(a) lisBook = [“The Hobbit”, “JRR Tolkien”, 1937, 3.99, True]
(b) print(lisBook)

Chapter 6 – Smooth operators

QUESTIONS

(a) addition + subtraction – multiplication * division /

EXERCISES

 myVar = 5
 myVar *= 4
 print (myVar) 

Chapter 7 – Making decisions

strUsername = input("What is your Username? ")
strPassword = input("What is your password? ")

if strUsername == "admin" and strPassword == "secret":
    print("You logon was successful, welcome.")
else:
    print ("Your Username or password is incorrect.")

Chapter 8 – Going loopy

(a)

lisSongs = ["design for life" , "driftwood", "karma police"]

for strSong in lisSongs:
    print (strSong)

(b) For loops and while loops
(c) Execution goes back to the beginning of the loop
(d) Stop further execution of the loop
(e) Nesting

Chapter 9 – Functions

QUESTIONS

(a) Procedure.
(b) Return.
(c) Parameters.
(d) Arguments.
(e) The scope of a variable is the parts of your code where the variable exists and is active.

Exercises

def inch2cent(floInch):
    floCent = floInch * 2.5
    return floCent

floInch = float( input ("Enter a measurement in inches: "))
floCent = inch2cent(floInch)
print (floCent)

Chapter 10 – OOP

QUESTIONS

(a) Methods.
(b) Fields.

Exercises

(a)

class Ball:
    strColor = "blue"

    def throw(self):
        print ("the ball is thrown")
    def bounce(self):
        print ("the ball bounces")

(b)

objTennisBall = Ball()
objTennisBall.strColor = "yellow"
print (objTennisBall.strColor)
objTennisBall.bounce()

Chapter 11 – Pygame

Exercises

(b)
Move your picture file into the project folder.
Find the following line and change ‘player1.gif’ to the filename of your image:
surPlayer = pygame.image.load(‘player1.gif’)

(c)
Move your .wav file into the project folder.
Find the following line, and change ‘sample1.wav’ to the filename of the music you downloaded:
pygame.mixer.music.load(‘sample1.wav’) #(‘house_lo.wav’)

(d)
Change the numbers in the following line of code to 0,0:
recAlien = Rect(450,200,surAlien.get_width(),surAlien.get_height())

Chapter 12 – Example game

(a)

if keyInput[K_UP]:
    recPlayer.y -= PLAYERSPEED
if keyInput[K_DOWN]:
    recPlayer.y += PLAYERSPEED

(b)

if keyInput[K_UP] and recPlayer.y > 0:
    recPlayer.y -= PLAYERSPEED
if keyInput[K_DOWN] and recPlayer.y <420:
    recPlayer.y += PLAYERSPEED