[Tutor] Classes: global name not defined

Ara Kooser ghashsnaga at gmail.com
Thu Sep 13 03:45:48 CEST 2007


   So taking the advice given my John, Kent, and Michael I reworked
the program and created a class for world. I was able to solve the
first several errors that came up as a I moved functions into class
methods but this one stumps me. The error I am getting is:

Traceback (most recent call last):
  File "/Users/ara/Documents/yeast/oop_yeast_nocaps.py", line 87, in <module>
    first_instance.print_world()
  File "/Users/ara/Documents/yeast/oop_yeast_nocaps.py", line 40, in print_world
    m, n = world['dimensions']
NameError: global name 'world' is not defined

I understand that the error refers to the method print_world(). In the
instance before first_instance.print_world() I define what world is
but that is not being carried over to the print_world method. In the
print world method do I need to refer back to the random_world method
so it knows what world is? Thank you.

CODE STARTS HERE:
import random
import copy

YEAST = '@'
EMPTY = '.'
GROUP = 'Q'


class World:
#What makes this a world

    def __init__(self,name):
        self.name = name
        #Add other things later

#Methods

    def percolation(self,perc):
#Sets up a percolation threshold value

        perc = float(perc)

        randval = random.random()
        if randval > perc:
            return EMPTY
        else:
            return YEAST

    def random_world(self):
#Constructs random world of size MxN
        world = {}
        for j in range(n):
            for i in range(m):
                world[i, j] = self.percolation(perc)
        world['dimensions'] = (m, n)
        return world

    def print_world(self):
#Prints out a string representation of a world.
        m, n = world['dimensions']
        for j in range(n):
            for i in range(m):
                print world[i, j],
            print

    def add_yeast(self):
#Allows the user to add a yeast cell at point m,n
        m,n = world['dimensions']
        new_world = copy.copy(world)

        counta = 0
        print "The upper left corner is the point (0,0)"
        yy = raw_input("How many yeast cells do you wish to add?")
        yy = int(yy)

        while counta<yy:
            i = int(raw_input("Please enter a m value for the yeast cell."))
            j = int(raw_input("Please enter a n value for the yeast cell."))

            new_world[i,j] = YEAST
            for j in range(n):
                for i in range(m):
                    world[i,j]=new_world[i,j]

            counta = counta+1

    def debug(self):
        print self.name


##########################################################
#Start of prgram
##########################################################

first_instance = World("small_world")
#first_instance.debug()

raw_input("Please press return to start the program.")

perc = raw_input("Please enter a thresold between 0-1 for the population.")
first_instance.percolation(perc)

n = int(raw_input("Please enter a n dimension.   "))
m = int(raw_input("Please enter a m dimension.   "))

first_instance.random_world()
first_instance.print_world()

first_instance.add_yeast()




-- 
Quis hic locus, quae regio, quae mundi plaga. Ubi sum. Sub ortu solis
an sub cardine glacialis ursae.


More information about the Tutor mailing list