[Tutor] dictionary and more

Jack Uretsky jlu at hep.anl.gov
Mon Oct 11 18:19:22 CEST 2010


Hi-
 	I apologize for the size of the attached file, but the question 
would make little sense if I made substantial deletions.
 	The program gives the following error:
Traceback (most recent call last):
   File "/Javastuff/python/Glau_Is.py", line 131, in <module>
     Adestin[state]()
TypeError: 'NoneType' object is not callable
 	This is after the RETA prinout from function A_1, so that seems to 
be called correctly.  The black screen is not part of the problem, I am 
not including the .jpeg files.  Line 131 is the one that reads
 	Adestin[state]()
and the program prints (correctly) state=1 before the traceback.
 		Thanks in advance for any insights.
 					JLU

"Trust me.  I have a lot of experience at this."
 		General Custer's unremembered message to his men,
 		just before leading them into the Little Big Horn Valley



-------------- next part --------------
 
import pygame
from pygame.locals import *
from sys import exit
import time
import random
from math import *
global gamma
gamma = .25
global RET
RET = [0,0]
global state
global r
def A_1():
    state =1
    r = 1 - gamma
    f = random.randrange(3)
    g = f+2  #destination
    RET = (g,r)
    print "RETA!=", RET
def A_2():
    state = 2
    global r2
    r2 = 1. + gamma
    f = random.randrange(3)
    if f == 0:
        g = 1 #destination
    elif f == 1:
        g,r2 = 7,1.
    elif f == 2:
        g,r2 = 6,1
    else:
        print "alarm 2"
    RET = [g,r2]
def A_3():    
    state =3
    global r3
    r3 = 1. + gamma
    f = random.randrange(3)
    if f == 1:
        g = 1
    elif f == 0:
        g,r3 = 7, 1.
    elif f == 2:
        g, r3 = 5, 1
    else:
        print "alarm 3"
    RET = [g, r3]
def A_4():
    state = 4
    global r4
    r4 = 1. + gamma
    f = random.randrange(3)
    if f == 2:
        g = 1
    elif f == 0:
        g,r4 = 6,1
    elif f == 1:
        g,r4 = 5,1
    else:
        print "alarm 4" 
    RET = [g, r4]
def A_5():
    state =5
    global r5
    r5 = 1. + gamma
    f = random.randrange(3)
    if f == 0:
        g = 8
    elif f == 1:
        g,r5 = 4,1
    elif f == 2:
        g,r5 = 3,1
    else:
        print "alarm 5"
    RET = [g, r5]
def A_6():
    state = 6
    global r6
    r6 = 1. + gamma
    f= random.randrange(3)
    if f == 1:
        g = 8
    elif f == 0:
        g,r6 = 4,1
    elif f == 2:
        g,r6 = 3,1
    else:
        print "alarm 6"
    RET = [g, r6]
def A_7():
    state = 7
    global r7
    r7 = 1. + gamma
    f = random.randrange(3)
    if f == 2:
        g = 8
    elif f == 0:
        g,r7 = 3,1
    elif f == 1:
        g,r7 = 2,1
    else:
        print "alarm 7"
    RET = [g, r7]
def A_8():
    state = 8
    global r8
    r8 = 1. - gamma
    f = random.randrange(3)
    g = f + 5
    RET = [g, r8]
#dictionaries
Adestin = {1: A_1(), 2 : A_2(), 3 : A_3(), 4: A_4(),5 : A_5(), 6 : A_6(), 7: A_7(), 8 :
 A_8()}
apics = {1: 'a_1.jpg', 2: 'a_2.jpg', 3 : 'a_3.jpg', 4: 'a_4.jpg',
         5: 'a_5.jpg', 6: 'a_6.jpg', 7 : 'a_7.jpg', 8 : 'a_8.jpg'}
state = 1 #set initial state
pygame.init()
screen = pygame.display.set_mode((640,480), 0, 32)
pygame.event.pump()
back1 =apics[state]
back = pygame.image.load(back1).convert()
screen.blit(back, (0,0))
pygame.display.update()
RET=(1,1-gamma)
k=0
while k <= 5:
    k = k+1
    back1 = apics[state]
    print "state=", state
    Adestin[state]()               
    print RET, k
    destin = RET[0]
    pois = RET[1]
    back = pygame.image.load(back1).convert()
    screen.blit(back, (0,0))
    pygame.display.update()
    state = destin
    print "state=",destin
    time.sleep(5)
print "end"
           
             


More information about the Tutor mailing list