Posts categorized “Uncategorized”.

16 Common Python Runtime Errors Beginners Find

Figuring out what Python’s error messages mean can be kind of tricky when you are first learning the language. Here’s a list of common errors that result in runtime error messages which will crash your program.

 

1) Forgetting to put a : at the end of an if, elif, else, for, while, class, or def statement. (Causes “SyntaxError: invalid syntax”)

This error happens with code like this:

if spam == 42
    print('Hello!')

 

2) Using = instead of ==. (Causes “SyntaxError: invalid syntax”)

The = is the assignment operator while == is the “is equal to” comparison operator. This error happens with code like this:

if spam = 42:
    print('Hello!')

 

3) Using the wrong amount of indentation. (Causes “IndentationError: unexpected indent” and “IndentationError: unindent does not match any outer indentation level” and “IndentationError: expected an indented block”)

Remember that the indentation only increases after a statement ending with a : colon, and afterwards must return to the previous indentation. This error happens with code like this:

print('Hello!')
    print('Howdy!')

…and this:

if spam == 42:
    print('Hello!')
  print('Howdy!')

…and this:

if spam == 42:
print('Hello!')

 

More… »

Stop Using “print” for Debugging: A 5 Minute Quickstart Guide to Python’s logging Module

  • This tutorial is short.
  • To figure out bugs in your code, you might put in print statements/print() calls to display the value of variables.
  • Don’t do this. Use the Python logging module.

The logging is better than printing because:

  • It’s easy to put a timestamp in each message, which is very handy.
  • You can have different levels of urgency for messages, and filter out less urgent messages.
  • When you want to later find/remove log messages, you won’t get them confused for real print() calls.
  • If you just print to a log file, it’s easy to leave the log function calls in and just ignore them when you don’t need them. (You don’t have to constantly pull out print() calls.)

Using print is for coders with too much time on their hands. Use logging instead. Also, learn to use the Python debugger to debug bugs and Pylint to prevent bugs and make your code readable.

To print log messages to the screen, copy and paste this code:

import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logging.debug('This is a log message.')

To write log messages to a file, you can copy and paste this code (the only difference is in bold):

import logging
logging.basicConfig(filename='log_filename.txt', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logging.debug('This is a log message.')

Later runs of the program will append to the end of the log file, rather than overwrite the file.

To log messages to a file AND printed to the screen, copy and paste the following:
More… »

My awesome exciting indie Pygame games: “Look At This Rock” and “Look At This Rock 2: A Different Rock”

Here are a couple games I wrote. The first was so popular that I made a sequel:

Win32 EXEs and source:

Look At This Rock

Look At This Rock 2: A Different Rock

Source (requires Pygame to be installed along with Python):

Source: LookAtThisRock.py and PNG

Source: LookAtThisRock2.py and PNG

Screenshots:

JavaScript Cipher Wheel

I’ve created a web version of the Caesar Cipher wheel using JQuery and CSS sprites.

JavaScript Cipher Wheel

I also have a Pygame version and Windows executable of this.

Great Pygame Example Site

I wanted to share this link to a great site with some simple Pygame examples:

Python and Pygame Examples

There is also a textbook draft called “Introduction to Computer Science Using Python and Pygame” by Paul Vincent Craven.

“MooseGesture” – Python Mouse Gestures Module

“MooseGesture” is a Python module that implements a basic mouse gesture recognition system. It can identify gestures made up of strokes in the eight cardinal and diagonal directions.

A mouse gesture is holding down the mouse button and moving the mouse cursor in a specific pattern to issue a command.
Mouse gestures are a way of dragging with the mouse in order to draw out a certain pattern. The most mainstream uses of mouse gestures in computer software are for web browsers. (Chrome, Firefox, Opera, Internet Explorer)

Mouse gestures can provide an interesting game mechanic that you can add to your own programs (similar to what the Wiimote does in a few games).

You can download the module (and a simple Pygame test script that uses it) here:

MooseGestures & Test App (zipped)

moosegesture.py

moosegesturetest.py

Simon Gesture – A game that uses the MooseGesture module.

(The screenshot above shows the test app after entering a mouse gesture. It correctly identifies the gesture as Down Right Up.)

(The above screenshot shows a more complicated gesture: Down, Up, Down, Right, Left, Right.)
More… »

Switch to our mobile site