MacLochlainns Weblog

Michael McLaughlin's Technical Blog

Site Admin

Archive for March, 2017

Python variable not defined

without comments

While working with a programming example for my students, I ran into an interesting run-time error when I changed their approach to importing Python’s random module. Here’s the raised error message:

Traceback (most recent call last):
  File "windowBouncingBalls.py", line 84, in <module>
    speed = [choice([-2,2]), choice([-2,2])]
NameError: name 'choice' is not defined

You raise the missing choice identifier when two things occur. The first thing requires you to use a standard import statement, like the following example, and the second thing requires you to continue to reference the identifier as “choice“.

import random

You can avoid the error by making the import of random like this:

from random import *

Or, you can leave the ordinary import statement and fully qualify the choice identifier with the random module name, like this:

    speed = [random.choice([-2,2]), random.choice([-2,2])]

As always, I hope this helps those who encounter a similar problem.

Written by maclochlainn

March 27th, 2017 at 12:23 am

Install PyGame on Fedora

without comments

The PyGame library is a wonderful tool for building games with Python. It lets you accomplish a great deal by simply managing events. You need to understand how to use Python functions, modules, and events to build games with this Python library.

You can download and install the PyGame library with the yum utility like this:

yum install -y pygame

It should generate the following list when you install it as the root user:

Loaded plugins: langpacks, refresh-packagekit
Available Packages
pygame.x86_64                        1.9.1-14.fc20                        fedora
[root@localhost ~]# yum install -y pygame
Loaded plugins: langpacks, refresh-packagekit
Resolving Dependencies
--> Running transaction check
---> Package pygame.x86_64 0:1.9.1-14.fc20 will be installed
--> Processing Dependency: numpy for package: pygame-1.9.1-14.fc20.x86_64
--> Processing Dependency: libportmidi.so.0()(64bit) for package: pygame-1.9.1-14.fc20.x86_64
--> Processing Dependency: libSDL_ttf-2.0.so.0()(64bit) for package: pygame-1.9.1-14.fc20.x86_64
--> Processing Dependency: libSDL_mixer-1.2.so.0()(64bit) for package: pygame-1.9.1-14.fc20.x86_64
--> Processing Dependency: libSDL_image-1.2.so.0()(64bit) for package: pygame-1.9.1-14.fc20.x86_64
--> Running transaction check
---> Package SDL_image.x86_64 0:1.2.12-7.fc20 will be installed
---> Package SDL_mixer.x86_64 0:1.2.12-5.fc20 will be installed
--> Processing Dependency: libmikmod for package: SDL_mixer-1.2.12-5.fc20.x86_64
---> Package SDL_ttf.x86_64 0:2.0.11-4.fc20 will be installed
---> Package numpy.x86_64 1:1.8.2-2.fc20 will be installed
--> Processing Dependency: python-nose for package: 1:numpy-1.8.2-2.fc20.x86_64
---> Package portmidi.x86_64 0:217-9.fc20 will be installed
--> Running transaction check
---> Package libmikmod.x86_64 0:3.3.6-3.fc20 will be installed
---> Package python-nose.noarch 0:1.3.0-1.fc20 will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
================================================================================
 Package            Arch          Version                  Repository      Size
================================================================================
Installing:
 pygame             x86_64        1.9.1-14.fc20            fedora         2.1 M
Installing for dependencies:
 SDL_image          x86_64        1.2.12-7.fc20            fedora          41 k
 SDL_mixer          x86_64        1.2.12-5.fc20            fedora          91 k
 SDL_ttf            x86_64        2.0.11-4.fc20            fedora          22 k
 libmikmod          x86_64        3.3.6-3.fc20             updates        142 k
 numpy              x86_64        1:1.8.2-2.fc20           updates        3.0 M
 portmidi           x86_64        217-9.fc20               fedora          26 k
 python-nose        noarch        1.3.0-1.fc20             fedora         272 k
 
Transaction Summary
================================================================================
Install  1 Package (+7 Dependent packages)
 
Total download size: 5.7 M
Installed size: 21 M
Downloading packages:
(1/8): SDL_image-1.2.12-7.fc20.x86_64.rpm                   |  41 kB  00:00     
(2/8): SDL_mixer-1.2.12-5.fc20.x86_64.rpm                   |  91 kB  00:00     
(3/8): portmidi-217-9.fc20.x86_64.rpm                       |  26 kB  00:00     
(4/8): SDL_ttf-2.0.11-4.fc20.x86_64.rpm                     |  22 kB  00:00     
(5/8): libmikmod-3.3.6-3.fc20.x86_64.rpm                    | 142 kB  00:00     
(6/8): numpy-1.8.2-2.fc20.x86_64.rpm                        | 3.0 MB  00:02     
(7/8): pygame-1.9.1-14.fc20.x86_64.rpm                      | 2.1 MB  00:01     
(8/8): python-nose-1.3.0-1.fc20.noarch.rpm                  | 272 kB  00:00     
--------------------------------------------------------------------------------
Total                                              1.7 MB/s | 5.7 MB  00:03     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction (shutdown inhibited)
  Installing : SDL_ttf-2.0.11-4.fc20.x86_64                                 1/8 
  Installing : SDL_image-1.2.12-7.fc20.x86_64                               2/8 
  Installing : portmidi-217-9.fc20.x86_64                                   3/8 
  Installing : libmikmod-3.3.6-3.fc20.x86_64                                4/8 
  Installing : SDL_mixer-1.2.12-5.fc20.x86_64                               5/8 
  Installing : python-nose-1.3.0-1.fc20.noarch                              6/8 
  Installing : 1:numpy-1.8.2-2.fc20.x86_64                                  7/8 
  Installing : pygame-1.9.1-14.fc20.x86_64                                  8/8 
  Verifying  : pygame-1.9.1-14.fc20.x86_64                                  1/8 
  Verifying  : SDL_mixer-1.2.12-5.fc20.x86_64                               2/8 
  Verifying  : python-nose-1.3.0-1.fc20.noarch                              3/8 
  Verifying  : libmikmod-3.3.6-3.fc20.x86_64                                4/8 
  Verifying  : 1:numpy-1.8.2-2.fc20.x86_64                                  5/8 
  Verifying  : portmidi-217-9.fc20.x86_64                                   6/8 
  Verifying  : SDL_image-1.2.12-7.fc20.x86_64                               7/8 
  Verifying  : SDL_ttf-2.0.11-4.fc20.x86_64                                 8/8 
 
Installed:
  pygame.x86_64 0:1.9.1-14.fc20                                                 
 
Dependency Installed:
  SDL_image.x86_64 0:1.2.12-7.fc20        SDL_mixer.x86_64 0:1.2.12-5.fc20      
  SDL_ttf.x86_64 0:2.0.11-4.fc20          libmikmod.x86_64 0:3.3.6-3.fc20       
  numpy.x86_64 1:1.8.2-2.fc20             portmidi.x86_64 0:217-9.fc20          
  python-nose.noarch 0:1.3.0-1.fc20      
 
Complete!

I hope this helps folks install the software.

Written by maclochlainn

March 25th, 2017 at 1:49 am