Challenge 3: Image Size

Suggested time: 20 minutes

A student is writing a program that lets you calculate the file size of a bitmap image. It currently has some errors. When it is fixed it should work as shown below:

Example program output when working correctly

The number of pixels in an image can be calculated by multiplying the width (in pixels) by the height (in pixels)

The number of bits used to store an image can be calculated by multiplying the total number of pixels by the number of bits per pixel.

The file size in bytes can be calculated by dividing the number of bits by 8

The file size in kibibytes can be calculated by dividing the number of bytes by 1024

Using the file Q02.py below:

Amend the code to add or complete lines to

  • Fix the ImportError on original line 6
    import Math
  • fix the logic error on original line 14
    BYTES_PER_KIBIBYTE = 1000
  • Fix the logic error to return the number of bytes for the image
  • Fix the syntax error on original line 42
    numberOfBytes = calcImageSize(imageWidthPx, imageHeightPx imageBitsPerPx)
  • Fix the logic error on original line 45
    numberOfKiB = numberOfBytes * BYTES_PER_KIBIBYTE

Do not add any additional functionality

If this was a real question it would tell you to save your code as Q02_FINISHED.py but you might want to save your code so you can show your teacher or come back to it later by pressing Ctrl + S or clicking on the icon that looks like this:

Save / share icon in create.withcode.uk lets you save your code