Indentation problem in my code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karmaB
    New Member
    • Mar 2007
    • 5

    #1

    Indentation problem in my code

    Hello!
    I'm a begginer at Python and I'm having some problems with my code.
    Can someone try and help me figuring out what is the problem??

    Code:
    class EndTrial(Dependent(SFBool)):
    def __init__(self):
    		SFBool.__init__(self)
        def evaluate(self, input):
            if KS.actionKeyRelease==17:
              write_file.write()
              experimentHandle.ftime.close()
              experimentHandle.fposition.close()
              experimentHandle.fforce.close()
              experimentHandle.screen=2
              SCREEN.whichChoice.set(2)
              QuitAPI()
    I'm getting an error message in the command line:
    "IndentationErr or: expected an idented block"
    with reference to the "if" line.

    But when I change the order of the lines, the error messege remain one line after the "evaluate" line so I think it might be the origin.
    Plus, there are little red dots before the if line...

    Lots of thanks!!
    May.
    Last edited by bartonc; Mar 26 '07, 09:16 AM. Reason: revised CODE tags
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by karmaB
    Hello!
    I'm a begginer at Python and I'm having some problems with my code.
    Can someone try and help me figuring out what is the problem??

    Code:
    class EndTrial(Dependent(SFBool)):
    def __init__(self):
    		SFBool.__init__(self)
        def evaluate(self, input):
            if KS.actionKeyRelease==17:
              write_file.write()
              experimentHandle.ftime.close()
              experimentHandle.fposition.close()
              experimentHandle.fforce.close()
              experimentHandle.screen=2
              SCREEN.whichChoice.set(2)
              QuitAPI()
    I'm getting an error message in the command line:
    "IndentationErr or: expected an idented block"
    with reference to the "if" line.

    But when I change the order of the lines, the error messege remain one line after the "evaluate" line so I think it might be the origin.
    Plus, there are little red dots before the if line...

    Lots of thanks!!
    May.
    Hi May. You only have 2 spaces for the indentation in the if block. I have re-indented your post. I also used CODE tags instead of HTML tags. Thanks for posting!
    Code:
    class EndTrial(Dependent(SFBool)):
    def __init__(self):
    		SFBool.__init__(self)
        def evaluate(self, input):
            if KS.actionKeyRelease==17:
                write_file.write()
                experimentHandle.ftime.close()
                experimentHandle.fposition.close()
                experimentHandle.fforce.close()
                experimentHandle.screen=2
                SCREEN.whichChoice.set(2)
                QuitAPI()

    Comment

    • ghostdog74
      Recognized Expert Contributor
      • Apr 2006
      • 511

      #3
      Like the error says, you have indentatoin problem
      Code:
      class EndTrial(Dependent(SFBool)):
          def __init__(self):
      	SFBool.__init__(self)
          def evaluate(self, input):	
              if KS.actionKeyRelease==17:
                write_file.write()
                experimentHandle.ftime.close()
                experimentHandle.fposition.close()
                experimentHandle.fforce.close()
                experimentHandle.screen=2
                SCREEN.whichChoice.set(2)
                QuitAPI()
      try to use spaces instead of tabs.

      Comment

      • karmaB
        New Member
        • Mar 2007
        • 5

        #4
        Thank you!
        It's working now.
        Since my Python knowledge isn't that good, I have problem with understanding the error messages.

        Now the message is:
        "AttributeError : MFNode instance has no attribute '__getitem__'

        My code:
        [HTML]import math
        import time
        import string

        #referencing ############### ############### ############### ############### ######
        DYN=references[1]
        TM=references[2]
        HD1=refernces[3]
        KS=references[4]
        TrialN=referenc es[5]
        SCREEN=referenc es[6]
        # referencing ############### ############### ############### ############### #####

        #ExperimentHand le - called every loop from X3D ############### ############### ###########
        class ExperimentHandl e(Dependent(SFV ec3f)):
        def __init__(self):
        SFVec3f.__init_ _(self)
        #making the folders for writing
        subject_name=ra w_input('\n Enter subjects name:')
        self.fposition= open('%s_positi on.txt'%subject _name,'w')
        self.ftime=open ('%s_time.txt'% subject_name,'w ')
        self.fforce=ope n('%s_force.txt '%subject_name, 'w')
        ##
        self.trial=0
        self.screen=0
        def evaluate(self,i nput):
        if self.screen==1:
        #getting the position
        self.pos=HD1.we ightedProxyPosi tion.get()
        TM.enabled.set( 1)
        #the time pos storing will be called now each routing cycle from the VRML

        #end of ExperimentHandl e class definition and creating the class instance ####
        experimentHandl e=ExperimentHan dle()

        #Time, position and velocity storing ############### ############### ##
        class Time_Pos_Storin g(Dependent(SFT ime)):
        def __init__(self):
        SFTime.__init__ (self)
        self.pos=[]
        self.time=[]
        self.flag_time= 1
        def evaluate(self,i npt):
        #position storing
        self.pos.append (experimentHand le.pos)
        #time storing
        if self.flag_time= =1:
        self.first_time _memory=TM.time .get()
        self.flag_time= 0
        self.time.appen d(0)
        else:
        self.time.appen d(TM.time.get()-self.first_time _memory)

        if len(self.pos)>= 1:
        self.current=se lf.pos[len(self.pos)-1]

        if self.current>-0.1:
        gForce.setGforc e()
        else:
        DYN.force.set(V ec3f(0,0,0))
        #end of Time, position and velocity storing ############### ##########
        t_p_stor=Time_P os_Storing()

        #GForce - creating gravity-like environment ############### #########
        class GForce:
        def __init__( self ):
        SFVec3f.__init_ _(self)
        self.forceLog=[]
        self.g = 9.806
        def setGforce( self ):
        mass=DYN.mass.g et()
        gravityForce=se lf.mass*self.g
        DYN.force.set(V ec3f(0,gravityF orce,0))
        #end of GForce class ############### ############### #####
        gForce=GForce()

        # Writting Data to txt files ############### ############### #########

        class WriteFile:
        def write(self):
        for i in t_p_stor.pos:
        experimentHandl e.fposition.wri te(str(i.y)+'\n ')
        for i in t_p_stor.time:
        experimentHandl e.ftime.write(s tr(i)+'\n')
        for i in gForce.forceLog :
        experimentHandl e.fforce.write( str(i)+'\n')
        # end of WriteFile ############### ############### ##############
        write_file=Writ eFile()

        # Initializing new trial ############### ############### #######
        class New_Trial:
        def initiate(self):
        ##writing all the lists to files
        write_file.writ e()
        ##initiating the list and the variables for next trial
        t_p_stor.pos=[]
        t_p_stor.time=[]
        gForce.forceLog =[]
        t_p_stor.flag_t ime=1
        ##writing a que in the files for ending a trial
        experimentHandl e.fposition.wri te('end of '+str(experimen tHandle.trial)+ 'trial \n')
        experimentHandl e.ftime.write(' end of '+str(experimen tHandle.trial)+ 'trial \n')
        experimentHandl e.fforce.write( 'end of '+str(experimen tHandle.trial)+ 'trial \n')
        TM.enabled.set( 0)

        # end of New_trial ############### ############### ###########
        new_trial=New_T rial()

        # Starting anf ending trials ############### ############### ######
        class EndTrial(Depend ent(SFBool)):
        def __init__(self):
        SFBool.__init__ (self)
        def evaluate(self, input):
        if KS.actionKeyRel ease==17:
        write_file.writ e()
        experimentHandl e.ftime.close()
        experimentHandl e.fposition.clo se()
        experimentHandl e.fforce.close( )
        experimentHandl e.screen=2
        SCREEN.whichCho ice.set(2)
        QuitAPI()

        endTrial=EndTri al()

        class StartTrial( Dependent(SFBoo l)):
        def __init__(self):
        SFBool.__init__ (self)
        def evaluate(self, input):
        if KS.actionKeyRel ease==16:
        experimentHandl e.screen=1
        SCREEN.whichCho ice.set(1)
        experimentHandl e.trial+=1
        TrialN.string.s et(str(experime ntHandle.trial) )
        QuitAPI()

        startTrial=Star tTrial()

        # end of EndTrial and StartTrial ############### ############[/HTML]

        Comment

        • karmaB
          New Member
          • Mar 2007
          • 5

          #5
          I forgot to mention that I work with H3D (3D software) as well so the references are from the .x3d file.

          I use Python for changing parameters and switching properties.

          Thanks again!

          Comment

          • karmaB
            New Member
            • Mar 2007
            • 5

            #6
            The full error message is:

            Traceback <mosr recent call last>:
            File "<string>", line 16, in ?
            AttributeError: MFNode instance has no atrribute '__getitem__'

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #7
              Originally posted by karmaB
              The full error message is:

              Traceback <mosr recent call last>:
              File "<string>", line 16, in ?
              AttributeError: MFNode instance has no atrribute '__getitem__'
              Well, it doesn't look like you've provided there pertainate snippet.

              First of all, reference is not defined.
              Secondly, an MFNode class object doesn't seem to be instantiated here.
              And "File "<string>", line 16, in ?" should tell us which file to look in, but (since it doesn't), I suspect it's in the library that you are using.

              Is there a chance that you can narrow down which assignment from references list (or tuple, or whatever) or which class creation is causing the error. Just get close enough to post the pertainate snippet. Thanks.

              I'll also need to change the title of this thread soon. Thanks for joining and welcome.

              Comment

              Working...