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;
$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()
```
python recursion homework fractals turtle-graphics
New contributor
$endgroup$
add a comment |
$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()
```
python recursion homework fractals turtle-graphics
New contributor
$endgroup$
add a comment |
$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()
```
python recursion homework fractals turtle-graphics
New contributor
$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
python recursion homework fractals turtle-graphics
New contributor
New contributor
edited 9 hours ago
200_success
135k21 gold badges173 silver badges443 bronze badges
135k21 gold badges173 silver badges443 bronze badges
New contributor
asked 11 hours ago
Brett HorstketterBrett Horstketter
233 bronze badges
233 bronze badges
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
Nice. I only have three comments.
In
draw_polygon()
use afor
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)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.).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
$endgroup$
add a comment |
$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)
$endgroup$
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
$begingroup$
Nice. I only have three comments.
In
draw_polygon()
use afor
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)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.).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
$endgroup$
add a comment |
$begingroup$
Nice. I only have three comments.
In
draw_polygon()
use afor
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)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.).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
$endgroup$
add a comment |
$begingroup$
Nice. I only have three comments.
In
draw_polygon()
use afor
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)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.).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
$endgroup$
Nice. I only have three comments.
In
draw_polygon()
use afor
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)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.).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
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
add a comment |
add a comment |
$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)
$endgroup$
add a comment |
$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)
$endgroup$
add a comment |
$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)
$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)
answered 9 hours ago
dariosicilydariosicily
1066 bronze badges
1066 bronze badges
add a comment |
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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