2016-08-25 145 views
0

我最近开发了一个Turtle程序来接收指令(turn,penup,penddown,circle等),并且工作正常。但是,当我尝试添加迭代一定数量的步骤的能力一定次数时,海龟程序将运行一次迭代,然后由于未知原因而无法响应。我认为这可能是因为程序迭代所需的'@',但没有任何实际工作。Python Turtle Not Responding ---没有明显的代码问题

本方案的条目的语法是:
˚F### - 用于转发 “###” 的长度
乙### - 向后为 “###” 的长度
大号### - 左转“###”程度
R ### - 右转“###”程度
C### - 用给定的(###)半径绘制圆圈
U - 拿起笔
D - 放下笔
I#... @ - 迭代指令块“#”次数

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() 
+0

*叹息* ...有一天,我将学习如何正确地输入代码到一个Stackoverflow的问题......但今天肯定不是那一天。 –

回答

1

我相信你的首要问题是这样一句话:commands.index("@")

这从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处理程序可以递归调用它来执行循环的主体。