15 Exercises

Chapter 5 – Variables

Questions

(a) What is the purpose of a variable?
(b) Name 3 different types of variables.
(c) What is the technical term for each item in a list?
(d) If you have a list within another list, what is this called?

Exercises

(a) Choose a book anyone will do, and create a list with the following information about that book:

– a string variable for holding the title
– a string variable for holding the author
– an integer variable (whole number) for holding the year it was published.
– a float variable (fractional number) for holding the price.
– a boolean variable to indicate whether or not there’s an ebook version.

(b) Print on screen the list you created in the last exercise.

Chapter 6 – Smooth operators

Questions

(a) What are the 4 arithmetic operators that are mentioned in this chapter?

Exercises

(a) Create a variable and give it the value 5. Using the shorthand format, multiply the variable by 4 and then print it on screen.

Chapter 7 – Making decisions

Exercises

(a) Ask for a username and password, storing these in string variables. Then check if the username = admin and the password = secret. If they match then print “Your login was successful, welcome.” otherwise print “Your Username or password is incorrect.”

Chapter 8 – Going loopy

Questions

(a) Create a list with three song titles in string variables. Use a for loop to print each song title to the screen.
(b) In the Python language two kinds of loop are available, what are they?
(c) What does the ‘continue’ command inside of a loop do?
(d) What does the ‘break’ command inside of a loop do?
(e) Having one loop inside another one, is called what?

Chapter 9 – Functions

Questions

(a) What is another name for a function?
(b) It’s possible for a function to pass a value back to the code which called it, which command does this?
(c) Inside the function definition, what are the variables called which RECEIVE values sent to a function?
(d) When values are SENT to a function, what name are they given?
(e) What is meant by a variable’s ‘scope?’

Exercises

(a) Write a function that converts inches into centimeters (use 1 inch = 2.5 cm) Ask the user to input a number of centimeters and then use the function to convert to inches and return the result. Print this on screen.

Chapter 10 – OOP

Questions

(a) Functions have another name when they’re part of an object, what are they called?
(b) Variables have another name when they’re part of an object, what are they called?

Exercises

(a) Create a class called Ball with the following methods and fields:

– a string field to hold the color of the ball, in this case blue.
– a throw method which simply prints ‘the ball is thrown.’
– a bounce method which prints ‘the ball bounces.’

(b) Create a tennis ball object from this class and:

– change the color of the tennis ball object to yellow, then print this field on the screen
– execute the tennis ball object’s bounce method

Chapter 11 – Pygame

Exercises

(a) Download the example project using the link below, extract the files and open ‘Pygame Example.py’

(b) Replace the player’s tank picture with a photo from your own collection or from the web.

It will probably need to be resized (use a graphics program or web site) down to about 100 pixels wide and 100 pixels high (it doesn’t have to be exact.) The file format should be .jpg .png or .gif.

(c) Change the background music to a song from your own collection or downlad one from the web. It has to be in WAVE format (filename ends with .wav)

(d) Move the position of the alien to the top left corner of the window.

Chapter 12 – Example game

Exercises

(a) Download the example project from the link below and extract the files (if you didn’t do that in the last exercise) then open ‘Example Game.py’

(b) Using the code which moves the player left and right as a guide, write 4 more lines to move the player up and down (put these below the left and right code.)

(c) Again, using the code which moves the player left and right as a guide: after writing the above commands to move the player up and down, add some conditional code that will restrict the player from moving off the top and bottom of the screen.