0

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

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
4

2 回答 2

0

我最终只是复制了函数并否定了其中的所有内容。多谢你们

于 2015-09-25T12:23:10.357 回答
0

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

 while True:

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

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

和类似的

于 2015-09-24T03:06:55.460 回答