#!/usr/bin/env python



import serial

import pygame



# allow multiple joysticks

joy = []



# handle joystick event

def handleJoyEvent(e):

    if e.type == pygame.JOYAXISMOTION:
        axis = "unknown"
        if (e.dict['axis'] == 0):
            axis = "X"

        if (e.dict['axis'] == 1):
            axis = "Y"

        if (e.dict['axis'] == 2):
            axis = "Throttle"

        if (e.dict['axis'] == 3):
            axis = "Z"

        if (axis != "unknown"):
            str = "Axis: %s; Value: %f" % (axis, e.dict['value'])
            # uncomment to debug
            #print str

    elif e.type == pygame.JOYBUTTONDOWN:
        str = "Button: %d" % (e.dict['button'])
        # uncomment to debug
        #output(str, e.dict['joy'])
        # Button 0 (trigger) to quit
        if (e.dict['button'] == 0):
            print "Bye!\n"
            quit()
    else:
        pass

# print the joystick position
def output(line, stick):
    print "Joystick: %d; %s" % (stick, line)

# wait for joystick input
def joystickControl():
    while True:
        e = pygame.event.wait()
        if (e.type == pygame.JOYAXISMOTION or e.type == pygame.JOYBUTTONDOWN):
            handleJoyEvent(e)

# main method
def joyinit():
    # initialize pygame
    pygame.joystick.init()
    pygame.display.init()
    if not pygame.joystick.get_count():
        print "\nPlease connect a joystick and run again.\n"
        quit()
    print "\n%d joystick(s) detected." % pygame.joystick.get_count()
    for i in range(pygame.joystick.get_count()):
        myjoy = pygame.joystick.Joystick(i)
        myjoy.init()
        joy.append(myjoy)
        print "Joystick %d: " % (i) + joy[i].get_name()
    print "Depress trigger (button 0) to quit.\n"

    # run joystick listener loop
    joystickControl()

# allow use as a module or standalone script
if __name__ == "__main__":
    joyinit()

@@ -17,17 +17,16 @@
 ython%0A%0A%0A
-%0A
 import s
@@ -48,17 +48,16 @@
 ygame%0A%0A%0A
-%0A
 # allow 
@@ -87,17 +87,16 @@
  = %5B%5D%0A%0A%0A
-%0A
 # handle
@@ -111,17 +111,16 @@
 k event%0A
-%0A
 def hand
@@ -134,17 +134,16 @@
 ent(e):%0A
-%0A
     if e
@@ -591,17 +591,16 @@
 int str%0A
-%0A
     elif
@@ -888,16 +888,17 @@
   pass%0A%0A
+%0A
 # print 
@@ -990,16 +990,17 @@
  line)%0A%0A
+%0A
 # wait f
@@ -1198,16 +1198,17 @@
 ent(e)%0A%0A
+%0A
 # main m
@@ -1770,16 +1770,16 @@
 er loop%0A
-
     joys
@@ -1793,16 +1793,17 @@
 trol()%0A%0A
+%0A
 # allow 

Make vertical whitespace more consistent