Python Turtle Not Responding ---代码中没有明显的问题

时间:2016-08-25 01:27:50

标签: python-3.x iteration turtle-graphics

我最近开发了一个Turtle程序来接收指令(转弯,平板,下降,圆圈等),并且工作正常。但是,当我尝试添加一定次数迭代一定数量的步骤的能力时,Turtle程序将运行一次迭代,然后由于未知原因而无法响应。我认为这可能是因为'@'是程序迭代所必需的,但没有任何东西真的有效。

该程序的输入语法是:
F ### - 转发“###”长度
B ### - 向后为“###”长度
L ### - 向左转“###”度
R ### - 向右转“###”度
C ### - 绘制一个给定(###)半径的圆圈 你 - 拿起笔 D - 将笔放下 我#... @ - 迭代指令块“#”次数

C250 I3 F050 R180 C225 @ B100 C125

CODE:

import turtle


def evaluate(commands):

    """
    It's basically supposed to work
    :param commands:
    :return: None
    """
    counter = 0
    commands_length = len(commands)
    while counter < commands_length:

        # If the letter U is encountered, the turtle goes up
        # counter increases by 2 to parse for the next character
        if commands[counter] == 'U':
            turtle.up()
            print('up')
            counter += 2

        # If the letter D is encountered, the turtle goes down
        # counter increases by 2 to parse for the next character
        elif commands[counter] == 'D':
            turtle.down()
            print('down')
            counter += 2

        # If the letter F is encountered, the turtle moves forward
        # by the amount denoted by the three following numbers
        elif commands[counter] == 'F':
            turtle.forward(int(commands[counter + 1: counter + 4: 1]))
            print('forward(' + commands[counter + 1: counter + 4: 1] + ')')
            counter += 5

        # If the letter C is encountered, the turtle draws a circle
        # with radius denoted by the three following numbers
        elif commands[counter] == 'C':
            turtle.circle(int(commands[counter + 1: counter + 4: 1]))
            print('circle(' + (commands[counter + 1: counter + 4: 1]) + ')')
            counter += 5

        # if the letter B is encountered, the turtle moves backward
        # by the amount denoted by the three following numbers
        elif commands[counter] == 'B':
            turtle.backward(int(commands[counter + 1: counter + 4: 1]))
            print('backward(' + (commands[counter + 1: counter + 4: 1]) + '}')
            counter += 5

        # if the letter L is encountered, the turtle turns to its left
        # by the angle denoted by the three following numbers
        elif commands[counter] == 'L':
            turtle.left(int(commands[counter + 1: counter + 4: 1]))
            print('left(' + (commands[counter + 1: counter + 4: 1]) + ')')
            counter += 5

        # if the letter R is encountered, the turtle turns to its right
        # by the angle denoted by the three following numbers
        elif commands[counter] == 'R':
            turtle.right(int(commands[counter + 1: counter + 4: 1]))
            print('right(' + (commands[counter + 1: counter + 4: 1]) + ')')
            counter += 5

        elif commands[counter] == 'I':
            counter += 3
            loop = commands[counter: commands.index("@") + 1]
            loop_counter = 0
            loop_length = len(loop)
            while loop_counter < loop_length:
                for _ in range(counter + (commands.index("@") - 1)):

                    for x in range(loop.index('@')):
                        # If the letter U is encountered, the turtle goes up
                        # counter increases by 2 to parse for the next character
                        if loop[loop_counter] == 'U':
                            turtle.up()
                            print('up')
                            loop_counter += 2

                        # If the letter D is encountered, the turtle goes down
                        # counter increases by 2 to parse for the next character
                        elif loop[loop_counter] == 'D':
                            turtle.down()
                            print('down')
                            loop_counter += 2

                        # If the letter F is encountered, the turtle moves forward
                        # by the amount denoted by the three following numbers
                        elif loop[loop_counter] == 'F':
                            turtle.forward(int(loop[loop_counter + 1: loop_counter + 4: 1]))
                            print('forward(' + loop[loop_counter + 1: loop_counter + 4: 1] + ')')
                            loop_counter += 5

                        # If the letter C is encountered, the turtle draws a circle
                        # with radius denoted by the three following numbers
                        elif loop[loop_counter] == 'C':
                            turtle.circle(int(loop[loop_counter + 1: loop_counter + 4: 1]))
                            print('circle(' + (loop[loop_counter + 1: loop_counter + 4: 1]) + ')')
                            loop_counter += 5

                        # if the letter B is encountered, the turtle moves backward
                        # by the amount denoted by the three following numbers
                        elif loop[loop_counter] == 'B':
                            turtle.backward(int(loop[loop_counter + 1: loop_counter + 4: 1]))
                            print('backward(' + (loop[loop_counter + 1: loop_counter + 4: 1]) + '}')
                            loop_counter += 5

                        # if the letter L is encountered, the turtle turns to its left
                        # by the angle denoted by the three following numbers
                        elif loop[loop_counter] == 'L':
                            turtle.left(int(loop[loop_counter + 1: loop_counter + 4: 1]))
                            print('left(' + (loop[loop_counter + 1: loop_counter + 4: 1]) + ')')
                            loop_counter += 5

                        # if the letter R is encountered, the turtle turns to its right
                        # by the angle denoted by the three following numbers
                        elif loop[loop_counter] == 'R':
                            turtle.right(int(loop[loop_counter + 1: loop_counter + 4: 1]))
                            print('right(' + (loop[loop_counter + 1: loop_counter + 4: 1]) + ')')
                            loop_counter += 5

    turtle.done()


def main() -> None:

    user_input = input("Enter Commands:")
    evaluate(user_input.upper())
    turtle.mainloop()

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:1)

我认为你的主要问题是这句话:commands

这从commands.index("@", counter)开始搜索,所以如果你有两个循环,第二个会找到错误的终结符。您需要从当前计数器开始搜索:turtle.done()

我也不相信你的重复代码是正确的。您对turtle.mainloop()的来电是可疑的,因为I是多余的,所以我不予理会。

下面只修改了您的I4 F050 L090 @ I6 L060 F030 @处理程序代码。您可以使用小型双循环程序对其进行测试:import turtle def evaluate(commands): """ It's basically supposed to work :param commands: :return: None """ counter = 0 commands_length = len(commands) while counter < commands_length: # ... if commands[counter] == 'I': repeat = int(commands[counter + 1: counter + 2: 1]) counter += 3 loop_end = commands.index("@", counter) loop = commands[counter: loop_end] counter = loop_end + 2 loop_length = len(loop) for _ in range(repeat): loop_counter = 0 while loop_counter < loop_length: # If the letter U is encountered, the turtle goes up # counter increases by 2 to parse for the next character if loop[loop_counter] == 'U': turtle.up() print('up') loop_counter += 2 # If the letter D is encountered, the turtle goes down # counter increases by 2 to parse for the next character elif loop[loop_counter] == 'D': turtle.down() print('down') loop_counter += 2 # If the letter F is encountered, the turtle moves forward # by the amount denoted by the three following numbers elif loop[loop_counter] == 'F': turtle.forward(int(loop[loop_counter + 1: loop_counter + 4: 1])) print('forward(' + loop[loop_counter + 1: loop_counter + 4: 1] + ')') loop_counter += 5 # If the letter C is encountered, the turtle draws a circle # with radius denoted by the three following numbers elif loop[loop_counter] == 'C': turtle.circle(int(loop[loop_counter + 1: loop_counter + 4: 1])) print('circle(' + (loop[loop_counter + 1: loop_counter + 4: 1]) + ')') loop_counter += 5 # if the letter B is encountered, the turtle moves backward # by the amount denoted by the three following numbers elif loop[loop_counter] == 'B': turtle.backward(int(loop[loop_counter + 1: loop_counter + 4: 1])) print('backward(' + (loop[loop_counter + 1: loop_counter + 4: 1]) + '}') loop_counter += 5 # if the letter L is encountered, the turtle turns to its left # by the angle denoted by the three following numbers elif loop[loop_counter] == 'L': turtle.left(int(loop[loop_counter + 1: loop_counter + 4: 1])) print('left(' + (loop[loop_counter + 1: loop_counter + 4: 1]) + ')') loop_counter += 5 # if the letter R is encountered, the turtle turns to its right # by the angle denoted by the three following numbers elif loop[loop_counter] == 'R': turtle.right(int(loop[loop_counter + 1: loop_counter + 4: 1])) print('right(' + (loop[loop_counter + 1: loop_counter + 4: 1]) + ')') loop_counter += 5 def main() -> None: user_input = input("Enter Commands:") evaluate(user_input.upper()) turtle.mainloop() if __name__ == '__main__': main()

I

我认为一个更好的解决方案,而不是重新实现evaluate()处理程序的所有内容,将设计I,以便 <img ng-repeat="imagePath in user.imagePath" ng-src="{{imagePath }}"> 处理程序可以递归地调用它来执行环路。