Sierpinski turtle triangleMenu system and TurtleSierpinski triangle from Pascal triangleSmall turtle program using instruction strings to draw patternsKoch snowflake in Python with TurtleDrawing fractals with TurtlePython the Dragon's Curve with turtleSolving Sierpinski Triangle in HaskellSymmetries of Triangle Python Turtle

Can a character who casts Shapechange and turns into a spellcaster use innate spellcasting to cast spells with a long casting time?

Does LaTeX redefine [some] TeX primitives?

Atari ST DRAM timing puzzle

Infeasibility in mathematical optimization models

What are good ways to improve as a writer other than writing courses?

How quickly could a country build a tall concrete wall around a city?

A question about 'reptiles and volatiles' to describe creatures

Blocking people from taking pictures of me with smartphone

In the movie Harry Potter and the Order or the Phoenix, why didn't Mr. Filch succeed to open the Room of Requirement if it's what he needed?

In Pokémon Go, why does one of my Pikachu have an option to evolve, but another one doesn't?

sed delete all the words before a match

Colleagues speaking another language and it impacts work

Is refreshing multiple times a test case for web applications?

Is TA-ing worth the opportunity cost?

How can you evade tax by getting employment income just in equity, then using this equity as collateral to take out loan?

A stranger from Norway wants to have money delivered to me

English - Acceptable use of parentheses in an author's name

Is it true that control+alt+delete only became a thing because IBM would not build Bill Gates a computer with a task manager button?

How would a family travel from Indiana to Texas in 1911?

Why should we care about syntactic proofs if we can show semantically that statements are true?

What is my malfunctioning AI harvesting from humans?

Are there any differences in causality between linear and logistic regression?

Pretty heat maps

Why couldn't soldiers sight their own weapons without officers' orders?



Sierpinski turtle triangle


Menu system and TurtleSierpinski triangle from Pascal triangleSmall turtle program using instruction strings to draw patternsKoch snowflake in Python with TurtleDrawing fractals with TurtlePython the Dragon's Curve with turtleSolving Sierpinski Triangle in HaskellSymmetries of Triangle Python Turtle






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








4












$begingroup$


I am working through Programming Fundamentals with Python on Udacity. The assignment was to create a recursive triangle. I am happy with the output, but I am hoping for feedback on the actual construction of the program. Could I have done this in a more simple and concise way?



import turtle

def draw_polygon(a_turtle, length, sides):
counter = 0
while (counter < sides):
a_turtle.forward(length)
a_turtle.right(360 / sides)
counter = counter + 1

def draw_triangle(a_turtle, length):
draw_polygon(a_turtle, length, 3)

def draw_fractal_triangle(a_turtle, length, depth):
if (depth == 1):
draw_triangle(a_turtle, length)
else:
for i in range(1,4):
draw_fractal_triangle(a_turtle, length/2, depth-1)
a_turtle.forward(length)
a_turtle.right(120)

def draw():
window = turtle.Screen()
window.bgcolor("black")

brent = turtle.Turtle()
brent.shape("turtle")
brent.color("yellow")

length = 200
brent.backward(length/2)
draw_fractal_triangle(brent, length, 5)
window.exitonclick()

draw()


```









share|improve this question









New contributor



Brett Horstketter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$




















    4












    $begingroup$


    I am working through Programming Fundamentals with Python on Udacity. The assignment was to create a recursive triangle. I am happy with the output, but I am hoping for feedback on the actual construction of the program. Could I have done this in a more simple and concise way?



    import turtle

    def draw_polygon(a_turtle, length, sides):
    counter = 0
    while (counter < sides):
    a_turtle.forward(length)
    a_turtle.right(360 / sides)
    counter = counter + 1

    def draw_triangle(a_turtle, length):
    draw_polygon(a_turtle, length, 3)

    def draw_fractal_triangle(a_turtle, length, depth):
    if (depth == 1):
    draw_triangle(a_turtle, length)
    else:
    for i in range(1,4):
    draw_fractal_triangle(a_turtle, length/2, depth-1)
    a_turtle.forward(length)
    a_turtle.right(120)

    def draw():
    window = turtle.Screen()
    window.bgcolor("black")

    brent = turtle.Turtle()
    brent.shape("turtle")
    brent.color("yellow")

    length = 200
    brent.backward(length/2)
    draw_fractal_triangle(brent, length, 5)
    window.exitonclick()

    draw()


    ```









    share|improve this question









    New contributor



    Brett Horstketter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






    $endgroup$
















      4












      4








      4





      $begingroup$


      I am working through Programming Fundamentals with Python on Udacity. The assignment was to create a recursive triangle. I am happy with the output, but I am hoping for feedback on the actual construction of the program. Could I have done this in a more simple and concise way?



      import turtle

      def draw_polygon(a_turtle, length, sides):
      counter = 0
      while (counter < sides):
      a_turtle.forward(length)
      a_turtle.right(360 / sides)
      counter = counter + 1

      def draw_triangle(a_turtle, length):
      draw_polygon(a_turtle, length, 3)

      def draw_fractal_triangle(a_turtle, length, depth):
      if (depth == 1):
      draw_triangle(a_turtle, length)
      else:
      for i in range(1,4):
      draw_fractal_triangle(a_turtle, length/2, depth-1)
      a_turtle.forward(length)
      a_turtle.right(120)

      def draw():
      window = turtle.Screen()
      window.bgcolor("black")

      brent = turtle.Turtle()
      brent.shape("turtle")
      brent.color("yellow")

      length = 200
      brent.backward(length/2)
      draw_fractal_triangle(brent, length, 5)
      window.exitonclick()

      draw()


      ```









      share|improve this question









      New contributor



      Brett Horstketter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      $endgroup$




      I am working through Programming Fundamentals with Python on Udacity. The assignment was to create a recursive triangle. I am happy with the output, but I am hoping for feedback on the actual construction of the program. Could I have done this in a more simple and concise way?



      import turtle

      def draw_polygon(a_turtle, length, sides):
      counter = 0
      while (counter < sides):
      a_turtle.forward(length)
      a_turtle.right(360 / sides)
      counter = counter + 1

      def draw_triangle(a_turtle, length):
      draw_polygon(a_turtle, length, 3)

      def draw_fractal_triangle(a_turtle, length, depth):
      if (depth == 1):
      draw_triangle(a_turtle, length)
      else:
      for i in range(1,4):
      draw_fractal_triangle(a_turtle, length/2, depth-1)
      a_turtle.forward(length)
      a_turtle.right(120)

      def draw():
      window = turtle.Screen()
      window.bgcolor("black")

      brent = turtle.Turtle()
      brent.shape("turtle")
      brent.color("yellow")

      length = 200
      brent.backward(length/2)
      draw_fractal_triangle(brent, length, 5)
      window.exitonclick()

      draw()


      ```






      python recursion homework fractals turtle-graphics






      share|improve this question









      New contributor



      Brett Horstketter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share|improve this question









      New contributor



      Brett Horstketter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share|improve this question




      share|improve this question








      edited 9 hours ago









      200_success

      135k21 gold badges173 silver badges443 bronze badges




      135k21 gold badges173 silver badges443 bronze badges






      New contributor



      Brett Horstketter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      asked 11 hours ago









      Brett HorstketterBrett Horstketter

      233 bronze badges




      233 bronze badges




      New contributor



      Brett Horstketter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




      New contributor




      Brett Horstketter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.

























          2 Answers
          2






          active

          oldest

          votes


















          2












          $begingroup$

          Nice. I only have three comments.




          1. In draw_polygon() use a for loop rather than an explicit counter:



            def draw_polygon(a_turtle, length, sides):
            for counter in range(sides):
            a_turtle.forward(length)
            a_turtle.right(360 / sides)



          2. Add comments and/or docstrings. These will help you when you look at the code in the future.



            For these kinds of tasks I usually add a top level docstring with a description of the task. Include a url to the problem if applicable. For example at the top of the file:



            '''Recursive implementation of Sierpinski Triangle

            Assignment from Programming Fundamentals with Python on Udacity.
            '''


            For any function/method etc. that has an algorithm that isn't immediately obvious from the code, add a comment or docstring explaining what/how it's doing it. For example, I would add a docstring to draw_recursive_triangle() to explain what the function is doing, any assumptions (does it matter which way the turtle is pointing?, are there min or max limits on length? are the triangles always equilateral?, etc.).




          3. The functions might be useful in another program/assignment. Rather than rewriting them, you could import this file as a library if you use a if __name__ == "__main__": guard like so:



            if __name__ == "__main__":
            draw()


            That way it runs the program if you execute the file, but not if you import it as a library







          share|improve this answer











          $endgroup$






















            2












            $begingroup$


            I'm a python beginner,so at the moment I can only see that you can slightly improve your code using range in the function draw_polygon, so instead of



             counter = 0
            while (counter < sides):
            a_turtle.forward(length)
            a_turtle.right(360 / sides)
            counter = counter + 1


            You can rewrite it in this way avoiding multiple divisions inside the loop and manual incrementing of variable counter:



            angle = 360 / sides
            for counter in range(sides):
            a_turtle.forward(length)
            a_turtle.right(angle)





            share|improve this answer









            $endgroup$

















              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "196"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );






              Brett Horstketter is a new contributor. Be nice, and check out our Code of Conduct.









              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f225847%2fsierpinski-turtle-triangle%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2












              $begingroup$

              Nice. I only have three comments.




              1. In draw_polygon() use a for loop rather than an explicit counter:



                def draw_polygon(a_turtle, length, sides):
                for counter in range(sides):
                a_turtle.forward(length)
                a_turtle.right(360 / sides)



              2. Add comments and/or docstrings. These will help you when you look at the code in the future.



                For these kinds of tasks I usually add a top level docstring with a description of the task. Include a url to the problem if applicable. For example at the top of the file:



                '''Recursive implementation of Sierpinski Triangle

                Assignment from Programming Fundamentals with Python on Udacity.
                '''


                For any function/method etc. that has an algorithm that isn't immediately obvious from the code, add a comment or docstring explaining what/how it's doing it. For example, I would add a docstring to draw_recursive_triangle() to explain what the function is doing, any assumptions (does it matter which way the turtle is pointing?, are there min or max limits on length? are the triangles always equilateral?, etc.).




              3. The functions might be useful in another program/assignment. Rather than rewriting them, you could import this file as a library if you use a if __name__ == "__main__": guard like so:



                if __name__ == "__main__":
                draw()


                That way it runs the program if you execute the file, but not if you import it as a library







              share|improve this answer











              $endgroup$



















                2












                $begingroup$

                Nice. I only have three comments.




                1. In draw_polygon() use a for loop rather than an explicit counter:



                  def draw_polygon(a_turtle, length, sides):
                  for counter in range(sides):
                  a_turtle.forward(length)
                  a_turtle.right(360 / sides)



                2. Add comments and/or docstrings. These will help you when you look at the code in the future.



                  For these kinds of tasks I usually add a top level docstring with a description of the task. Include a url to the problem if applicable. For example at the top of the file:



                  '''Recursive implementation of Sierpinski Triangle

                  Assignment from Programming Fundamentals with Python on Udacity.
                  '''


                  For any function/method etc. that has an algorithm that isn't immediately obvious from the code, add a comment or docstring explaining what/how it's doing it. For example, I would add a docstring to draw_recursive_triangle() to explain what the function is doing, any assumptions (does it matter which way the turtle is pointing?, are there min or max limits on length? are the triangles always equilateral?, etc.).




                3. The functions might be useful in another program/assignment. Rather than rewriting them, you could import this file as a library if you use a if __name__ == "__main__": guard like so:



                  if __name__ == "__main__":
                  draw()


                  That way it runs the program if you execute the file, but not if you import it as a library







                share|improve this answer











                $endgroup$

















                  2












                  2








                  2





                  $begingroup$

                  Nice. I only have three comments.




                  1. In draw_polygon() use a for loop rather than an explicit counter:



                    def draw_polygon(a_turtle, length, sides):
                    for counter in range(sides):
                    a_turtle.forward(length)
                    a_turtle.right(360 / sides)



                  2. Add comments and/or docstrings. These will help you when you look at the code in the future.



                    For these kinds of tasks I usually add a top level docstring with a description of the task. Include a url to the problem if applicable. For example at the top of the file:



                    '''Recursive implementation of Sierpinski Triangle

                    Assignment from Programming Fundamentals with Python on Udacity.
                    '''


                    For any function/method etc. that has an algorithm that isn't immediately obvious from the code, add a comment or docstring explaining what/how it's doing it. For example, I would add a docstring to draw_recursive_triangle() to explain what the function is doing, any assumptions (does it matter which way the turtle is pointing?, are there min or max limits on length? are the triangles always equilateral?, etc.).




                  3. The functions might be useful in another program/assignment. Rather than rewriting them, you could import this file as a library if you use a if __name__ == "__main__": guard like so:



                    if __name__ == "__main__":
                    draw()


                    That way it runs the program if you execute the file, but not if you import it as a library







                  share|improve this answer











                  $endgroup$



                  Nice. I only have three comments.




                  1. In draw_polygon() use a for loop rather than an explicit counter:



                    def draw_polygon(a_turtle, length, sides):
                    for counter in range(sides):
                    a_turtle.forward(length)
                    a_turtle.right(360 / sides)



                  2. Add comments and/or docstrings. These will help you when you look at the code in the future.



                    For these kinds of tasks I usually add a top level docstring with a description of the task. Include a url to the problem if applicable. For example at the top of the file:



                    '''Recursive implementation of Sierpinski Triangle

                    Assignment from Programming Fundamentals with Python on Udacity.
                    '''


                    For any function/method etc. that has an algorithm that isn't immediately obvious from the code, add a comment or docstring explaining what/how it's doing it. For example, I would add a docstring to draw_recursive_triangle() to explain what the function is doing, any assumptions (does it matter which way the turtle is pointing?, are there min or max limits on length? are the triangles always equilateral?, etc.).




                  3. The functions might be useful in another program/assignment. Rather than rewriting them, you could import this file as a library if you use a if __name__ == "__main__": guard like so:



                    if __name__ == "__main__":
                    draw()


                    That way it runs the program if you execute the file, but not if you import it as a library








                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 9 hours ago









                  200_success

                  135k21 gold badges173 silver badges443 bronze badges




                  135k21 gold badges173 silver badges443 bronze badges










                  answered 9 hours ago









                  RootTwoRootTwo

                  9543 silver badges6 bronze badges




                  9543 silver badges6 bronze badges


























                      2












                      $begingroup$


                      I'm a python beginner,so at the moment I can only see that you can slightly improve your code using range in the function draw_polygon, so instead of



                       counter = 0
                      while (counter < sides):
                      a_turtle.forward(length)
                      a_turtle.right(360 / sides)
                      counter = counter + 1


                      You can rewrite it in this way avoiding multiple divisions inside the loop and manual incrementing of variable counter:



                      angle = 360 / sides
                      for counter in range(sides):
                      a_turtle.forward(length)
                      a_turtle.right(angle)





                      share|improve this answer









                      $endgroup$



















                        2












                        $begingroup$


                        I'm a python beginner,so at the moment I can only see that you can slightly improve your code using range in the function draw_polygon, so instead of



                         counter = 0
                        while (counter < sides):
                        a_turtle.forward(length)
                        a_turtle.right(360 / sides)
                        counter = counter + 1


                        You can rewrite it in this way avoiding multiple divisions inside the loop and manual incrementing of variable counter:



                        angle = 360 / sides
                        for counter in range(sides):
                        a_turtle.forward(length)
                        a_turtle.right(angle)





                        share|improve this answer









                        $endgroup$

















                          2












                          2








                          2





                          $begingroup$


                          I'm a python beginner,so at the moment I can only see that you can slightly improve your code using range in the function draw_polygon, so instead of



                           counter = 0
                          while (counter < sides):
                          a_turtle.forward(length)
                          a_turtle.right(360 / sides)
                          counter = counter + 1


                          You can rewrite it in this way avoiding multiple divisions inside the loop and manual incrementing of variable counter:



                          angle = 360 / sides
                          for counter in range(sides):
                          a_turtle.forward(length)
                          a_turtle.right(angle)





                          share|improve this answer









                          $endgroup$




                          I'm a python beginner,so at the moment I can only see that you can slightly improve your code using range in the function draw_polygon, so instead of



                           counter = 0
                          while (counter < sides):
                          a_turtle.forward(length)
                          a_turtle.right(360 / sides)
                          counter = counter + 1


                          You can rewrite it in this way avoiding multiple divisions inside the loop and manual incrementing of variable counter:



                          angle = 360 / sides
                          for counter in range(sides):
                          a_turtle.forward(length)
                          a_turtle.right(angle)






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 9 hours ago









                          dariosicilydariosicily

                          1066 bronze badges




                          1066 bronze badges























                              Brett Horstketter is a new contributor. Be nice, and check out our Code of Conduct.









                              draft saved

                              draft discarded


















                              Brett Horstketter is a new contributor. Be nice, and check out our Code of Conduct.












                              Brett Horstketter is a new contributor. Be nice, and check out our Code of Conduct.











                              Brett Horstketter is a new contributor. Be nice, and check out our Code of Conduct.














                              Thanks for contributing an answer to Code Review Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              Use MathJax to format equations. MathJax reference.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f225847%2fsierpinski-turtle-triangle%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                              Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                              Ласкавець круглолистий Зміст Опис | Поширення | Галерея | Примітки | Посилання | Навігаційне меню58171138361-22960890446Bupleurum rotundifoliumEuro+Med PlantbasePlants of the World Online — Kew ScienceGermplasm Resources Information Network (GRIN)Ласкавецькн. VI : Літери Ком — Левиправивши або дописавши її