0

Is there a way to negate a function so it returns negatives. In my function, I have conditionals and each makes this "turtle" move. Is there a way to negate this so the every turtle move is negative. I am speaking of '=' condition.

def ttInterpret( program ):
            """
            interpret program as a TinyTurtle language string.
            program -- a TinyTurtle string, possibly with string manipulation symbols
            """
            stor_pos = turtle.pos()
            spec_index = 0
            for ch in program:
                if ch >= '0' and ch <= '9':
                    turtle.fd(int(ch) * 10)
                elif ch == '<':
                    turtle.left(15)
                elif ch == 'L':
                    turtle.left(90)
                elif ch == '>':
                    turtle.right(15)
                elif ch == 'R':
                    turtle.right(90)
                elif ch == ' ':
                    pass

                elif ch == '@':
                    # ttInterpret( program[:program.index(ch):-1] )
                    stor_pos = turtle.pos() #keeps track of when @ was used
                    spec_index = program.index( ch ) #returns the index of the most recent specified character

                elif ch == '/':
                    fds_index = program.index( ch ) #returns last '/' index
                    ttInterpret( program[spec_index:fds_index - 1] )
                    # while ch != '/':
                        # ttInterpret( -program[ch::-1] )
                elif ch == '!':
                    turtle.setpos(stor_pos)

                elif ch == '=':
                    ttInterpret( program[:int(program.index(ch)):-1] ) #returns to start
                    ttInterpret( program[:int(program.index(ch)):1] ) #mirrors from start

                else:
                    print("Error: ", ch," is not supported by TinyTurtle")
            return
matttm
  • 124
  • 1
  • 2
  • 15
  • Please [fix the *code block*](http://meta.stackexchange.com/q/22186). – wwii Sep 24 '15 at 02:12
  • I am trying to negate the ttinterpret func in the = case, and it is a recursive call. (its meant to mirror the image, btw) – matttm Sep 24 '15 at 02:23
  • You could use dictionaries to map characters to turtle methods - one for *forward* and one for *reverse*, then switch the dictionaries when needed. – wwii Sep 24 '15 at 02:26

2 Answers2

0

Put the whole code within a while loop, as below.

 while True:

Then, instead of calling the same function recursively, use:

program = program[:int(program.index(ch)):-1] 
continue #returns to start

And similar

Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
0

I ended up just duplicating the function and negating everything in it. Thanks guys

matttm
  • 124
  • 1
  • 2
  • 15