否定python

时间:2015-09-24 02:09:55

标签: python turtle-graphics negation

有没有办法否定一个函数,所以它返回负数。在我的功能中,我有条件,每个都有这个" turtle"移动。有没有办法否定这一点,所以每次乌龟移动都是消极的。我说的是' ='条件。

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

2 个答案:

答案 0 :(得分:0)

将整个代码放在while循环中,如下所示。

 while True:

然后,不要递归地调用相同的函数,而是使用:

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

和类似的

答案 1 :(得分:0)

我最终只是复制了这个功能并否定了它中的所有内容。谢谢你们