Home > Books > Mission Python > How the checksums work

Mission Python: How do the checksums work?

A reader wrote to ask me how the checksums work in the following code from Listing 6-1 in Chapter 6:

checksum = 0
check_counter = 0
for key, room_scenery_list in scenery.items():
     for scenery_item_list in room_scenery_list:
         checksum += (scenery_item_list[0] * key
             + scenery_item_list[1] * (key + 1)
             + scenery_item_list[2] * (key + 2))
         check_counter += 1
print(check_counter, "scenery items")
assert check_counter == 161, "Expected 161 scenery items"
assert checksum == 200095, "Error in scenery data"
print("Scenery checksum: " + str(checksum))

They wrote:

I'm new to coding. Mission Python is my first attempt. I'm enjoying it so far and I like the detailed explanations you give for each listing. However, I'm stuck at page 101 in Chapter 6 with the check sum and check counter part. I was wondering why you used a multiplication of key and * key+1 and *key+2. Furthermore, why was the check counter 0 and then became 1? Can you please take me through this part? Thank you so much for the book!
Screenshot of the space adventure game from Mission Python, showing an astronaut in a room with three beds and three energy balls they will have to dodge

There wasn’t space to go into this in the book, unfortunately. The checksum is there to help readers avoid any errors when they're typing in the listings. It's not really part of the game code so it was a natural candidate for cutting when we ran out of space.

The goal of the checksum is to identify whether any numbers are wrong, or are in the wrong place. Multiplying the numbers helps to identify if numbers are in the wrong place.

For example, if we had the numbers 1,2,3, just adding them together would give us 6. If we got the numbers in the wrong order of 2,1,3 that would still give us a checksum total of 6 even though the data was in the wrong order.

By multiplying each number (shown first below) by its position (shown after the * below), we reduce the change of that happening:
1*1 = 1
2*2 = 4
3*3 = 9
The new checksum total is 14

If we have the data in the wrong order (2,1,3):
2*1 = 2
1*2 = 2
The new checksum total is 13. So we’re now able to detect that the first two numbers are in the wrong order.

It doesn’t always work (you can find ways to jumble up the number that still generate the same checksum sometimes), but it nearly always works and is highly likely to catch accidental errors.

In the book, I’m actually multiplying the first number by the room number (which comes through in the key variable), and the second number by the room number plus 1, and the third number by the room number plus 2. So for room number 31, the three pieces of scenery data for each item are multiplied by 31, 32, 33.

The check counter is used to count how many scenery items are identified in case one is missed out altogether. Before going through the list to check, it’s set to 0. Each time an item of scenery is added to the checksum the check counter variable is increased by 1. The += operator adds one to a number.

check_counter = 0 # sets the variable to 0
check_counter += 1 # adds 1 to whatever value the check counter has, so it keeps a running total

Without the checksum code, readers could have entered incorrect game data, which might have caused errors later on or made the game impossible to complete. Of course, there's no need to retype all the code: the listings are all available to download, so you can use them or copy and paste parts from them as you build the game (shown above).

How the loops work

Another reader wrote:

I recently purchased "Mission Python" and I am enjoying the experience of coding the game. I'm wondering if you could direct me to an explanation of the following code: I see how the checksum operates, but I am unclear on the structure of the "for" loop and how it works.

The outer loop works its way through the scenery dictionary. It takes each entry in turn and puts its key (room number) into the variable key. The list of lists that contains all of the room’s scenery data goes into room_scenery_list. This outer loop is picking the entry for a room from the dictionary.

The inner loop then goes through the room_scenery_list. Each item in that list is itself a list of three numbers that positions one piece of furniture. Each “piece of furniture” list goes into the scenery_item_list. This inner loop is picking each piece of furniture from the room’s scenery data.

The body of the checksum then uses list indexes to access the individual numbers in the furniture list.

I made a simple demo program that might be easier to follow, with more generic variable names and simpler data values for checking.

data = {
     7: [[1,2,3], [4,5,6]],
     12: [[7,8,9], [10,11,12]]

for key, list_of_lists in data.items():
     print("Dictionary key is " + str(key))
     print("List of lists for that key is:")
     for specific_list in list_of_lists:
         print("Next list broken out from that list of lists is")
         print("First letter in that list is")

Find out more


© Sean McManus. All rights reserved.

Visit www.sean.co.uk for free chapters from Sean's coding books (including Mission Python, Scratch Programming in Easy Steps and Coder Academy) and more!

Discover my latest books

Coding Compendium

Coding Compendium

A free 100-page ebook collecting my projects and tutorials for Raspberry Pi, micro:bit, Scratch and Python. Simply join my newsletter to download it.

Web Design in Easy Steps

Web Design IES

Web Design in Easy Steps, now in its 7th Edition, shows you how to make effective websites that work on any device.

100 Top Tips: Microsoft Excel

100 Top Tips: Microsoft Excel

Power up your Microsoft Excel skills with this powerful pocket-sized book of tips that will save you time and help you learn more from your spreadsheets.

Scratch Programming in Easy Steps

Scratch Programming IES

This book, now fully updated for Scratch 3, will take you from the basics of the Scratch language into the depths of its more advanced features. A great way to start programming.

Mission Python book

Mission Python

Code a space adventure game in this Python programming book published by No Starch Press.

Cool Scratch Projects in Easy Steps book

Cool Scratch Projects in Easy Steps

Discover how to make 3D games, create mazes, build a drum machine, make a game with cartoon animals and more!

Walking astronaut from Mission Python book Top | Search | Help | Privacy | Access Keys | Contact me
Home | Newsletter | Blog | Copywriting Services | Books | Free book chapters | Articles | Music | Photos | Games | Shop | About