LPTHW – Lesson 33, 34, 35
These lessons covered while loops and list indexes, but they also combined a few concepts from the previous lessons into a more complex exercise (another game). I’m noticing some recurring themes in these games by the way… Bears, Cthulhu… Anyway.
Interestingly, one of the most useful things I came across today was by accident. If you hit the up arrow in PowerShell – or any other terminal program, I think – it will pull up your previous commands. This can save a TON of time if you are using the same commands over and over, like I am to create new Python files for these exercises.
The “bear game” also taught me some other interesting lessons:
- If you are using a boolean variable , you can just say variable or not variable to check True/False instead of variable == True or variable == False.
- You can either use “while True:” (like in the bear room) or a recursive call to the current function (as in the cthulhu room) to create an infinite loop.
- It is useful to create small functions for actions that will be repeated frequently by the program.
- State variables (bear_moved) can be useful to track changes in the program.
- You can use int(raw_input()) and Try/Except ValueError: to check if a number was entered by a user.
- One other note from lesson 34… Indexes start at zero! So in letters = [‘a’, ‘b’, ‘c’, ‘d’], letters[0] = a