How we use algorithms

Many students had difficulty figuring out how to code the number guessing game in class on Monday, Feb. 24.

You already know ALL of the building blocks of programming. This is (pretty much) everything:

  • variables
  • functions
  • if statements
  • loops (for and while)

So here is something else to think about:


An algorithm is like a recipe: a step-by-step process for performing some activity. You may look at an algorithm as the steps your program goes through to solve a problem. For example:

High Score Algorithm

  1. Game is over
  2. Compare player’s score to high score
  3. If player’s score is greater than the high score then the high score variable is reassigned the player’s score
  4. Display new high score

There are algorithms that exist for many common programming problems such as sorts and indexes. (source)

This is the problem-solving method you will need to develop. When I told you “break it down,” this shows one example of breaking down a problem that needs to be solved. What do I want to do? I want to tell the player if she has the high score. How do I find out of she has the high score? And so on.

You must first reason through the TASKS you want to do. THEN think about the code you will need to write to make it happen.


Imagine a dice-throwing game: Any player who gets doubles will win.

  1. Player throws two dice.
  2. Computer throws two dice.
  3. Did both get doubles? (What happens?)
  4. Did only one get doubles? (What happens?)
  5. Did no one get doubles? (What happens?)

Now, the algorithm:

  1. Throw two dice for the player. Save two numbers, possible 1 through 6 for each.
  2. Throw two dice for the computer. Save two numbers, possible1 through 6 for each.
  3. Compare two faces of the player’s dice to check for doubles.
  4. Compare two faces of the computer’s dice to check for doubles.
  5. If no one has doubles, announce or print that.
  6. If both have doubles, announce that it is a tie.
  7. If the human has doubles, announce that the human wins.
  8. If the computer has doubles, announce that the computer wins.

The order of 5 through 8 might change, depending on how you want to write your if-statement.

Here is another example (a very simple one).

You can do this in either Python OR JavaScript! Give it a try!