2016-08-25 10 views
0

最近、私は指導(ターン、ペップアップ、ペンダウン、サークルなど)を受け取るタートルプログラムを開発しました。しかし、特定の回数のステップを反復する機能を追加しようとすると、Turtleプログラムは1回の繰り返しで実行され、不明な理由で応答できなくなります。私はそれがプログラムのために必要な '@'のためかもしれないと思うが、何も実際に働いていない。プログラムPython Turtle Not Responding ---コードに明らかな問題はありません

エントリの構文は次のとおり
F ### - "###" の長
Lの###の下位 - "###" の長
B ###のための順方向 - "###"度で左に曲げる
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("@")

次の2つを持っている場合、これはそうcommandsの先頭から検索2番目のものは間違ったターミネータを見つけます。現在のカウンターから検索する必要があります:commands.index("@", counter)

あなたの繰り返しコードが正しいとは思いません。 turtle.done()へのあなたの電話はturtle.mainloop()と重複しているので不審なので、私はそれを残しておきます。

以下は、Iハンドラコードのリワークです。私はよりよい解決策を考えるのではなく、Iハンドラのためにすべてを再実装、Iハンドラを実行するために再帰的に呼び出すことができevaluate()ように設計するのだろう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() 

:あなたは小さなダブルループプログラムでそれをテストすることができますループの本体