Welcome to Mark's blog for Capture The Flag (CTF) enthusiasts

Thursday, June 16, 2022

Hacker Hangman

Note: The code used in Hacker Hangman was inspired by  https://inventwithpython.com/invent4thed/chapter8.html. This was one of the books that got me started on Python. The website also has instructions for you to make many other exciting games. 

This page was supposed to show code for a fun cybersecurity-themed hangman game, but some evil hackers have messed the code up. Let's fix it together.

The game was written in Python. There are three common problems you might find in Python code:

Syntax Error
A syntax happens when your code doesn't follow the basic 'rules' of Python. Syntax errors are the most common and easiest to fix. Usually, the entire program will halt, and you'll get some error text instead.

Take this sentence: "End everything with a. period"

You can tell immediately that something's wrong. A period (full stop) should come at the end of a sentence, not in the middle.

Similarly in Python:
counter =+ 1

This will create a syntax error. The proper way to add one is using "+=", not "=+". That's just the way the creators of Python decided to do it, just like how the creators of the English language decided periods come at the end of a sentence.

Just fix the wrong symbols, so that the statement becomes counter += 1.


Type Error
A type error happens when you try to make Python compare two different types of objects. This is like comparing apples to bananas, and Python is smart enough to realize there's no way it can do what you're telling it to. So it stops running your code, and gives you a nice error message instead.

Take this sentence: "Simon was feeling tiredly."

There are no problems with the punctuation, but the grammar isn't correct. "Tiredly" is an adverb, the correct sentence would be "Simon was feeling tired."

Similarly, the Python code:
p='99'
p-1
Would produce a Type Error. The variable 'p' is set to a string ('potatoes'). You can't subtract one from a string, because a string isn't the right type for that. Instead, you should convert the variable 'p' to a suitable type first, like an integer (whole number).

Fixed Python code:
p=int(''99') #convert p to an integer first.
p-1

Logic Error
A logic error happens when your whole program executes successfully, but doesn't produce the expected output. These are probably the hardest to fix. Syntax and type errors usually cause the whole program to stop running, and you get bright red text instead showing you what's wrong.

Take these two sentences:
1. James was cycling to school.
2. James saw an orange cat.

A logic error would be combining them to form the sentence
"James saw an orange cat cycling to school."

This sentence doesn't break any grammar or punctuation rules, but it makes no sense logically. A cat can't ride a bicycle, can it? (At least, I've never seen one that could.)

Similarly, the Python code

originalCats=1
newCatsBought=3
print("James used to have", originalCats ,"cat. Now he has", newCatsBought ,"cats.")
Would execute fully, but the output wouldn't correct. James should have 4 cats now, not 3. The correct code would be

print("James used to have", originalCats ,"cat. Now he has", newCatsBought + originalCats ,"cats.")


It's your turn!
Find the code for this activity at:
There are 2 syntax errors, 2 type errors and 2 logic errors that you need to fix.

Once you're done fixing all the errors, show off your code to your friends and challenge them to a game of Hacker Hangman!  

Answers
If you're stuck, click this button to check the answers.