Stage 2: Build a MadLib game

Introduction to serious programming

Here are some of the topics covered in this lesson:

Variables

Functions

Strings

Strings are a way to convey information. They can also be indexed, i.e. each character in the string has a number attached to it, which makes it convenient for manipulation. We can use the square brackets and the find function to perform certain operations:

While Loops

While loops keep executing as long as their test condition is true. This a very convenient way of avoiding repetition and hard-coding. It is formulated as follows:
while (condition): rest of your code here.
You can also insert the break command to exit the loop before it ends. This is useful to break an infinite loop.

Debugging

There will always be errors in the code. It is just a fact of life that has to be acknowledged and dealt with. Debugging should really be treated as part of the developement process and as such, be handled with a systematic approach:

  1. Examine error messages when programs crash: the last line is usually the most understandable
  2. Work from example code: it is easy to find example code on the internet and use it as starting point. However...
  3. Make sure examples work!
  4. Check (print) intermediate results: you can use the print statement to make sure that the content of your variables evolves as expected as the program executes
  5. Keep and compare old versions: you can either comment out the old version or use GIT to capture the exact state of your code at a certain point in time

For lists and For loops

Lists are a way of structuring and packaging data. They can contain strings, numbers and other lists. I suspect that they are also called arrays.
Lists can be accessed, concatenated, appened to or have elements removed.


Lists can also be altered by mutation and aliasing.



Then there the "For" loops that enable you to iterate throught the list



On a final note about Python, you should be aware that the language is subject to formating rules reffered to as "PEP 8" and that you can check your code here: pep 8 checker

How to solve problems

  1. Don't panic
  2. Understand the problem, i.e. inputs and outputs
  3. What are the valid inputs?
    • Have you defined the validity criteria of the imput?
    • Are you testing the validity of the input?
    • How are you going to represent the inputs, i.e. how will they be packaged? Seperate values, objects...?
  4. What are the outputs?
  5. What are the relationships between the inputs and the outputs? You should work it out by working through some examples that you can use as test cases later on.
  6. Understand how a human systematically solves the problem
  7. Write the algorithm in pseudo code
  8. See if there is any way to simplify the way the product is being handled. The first attempt is rarely the best one as we often try to take shortcuts that will come back to bite us at some point in time... Use a simpe mechanical solution
  9. Don't optimise too early...
  10. Develop incrementally by testing often