This is my first time ever using turtle, so bear with me. I am suppose to make a tree diagram in python using turtle. I made the tree and it looks perfect except for one problem, which might seem really simple but, when I print out my tree it looks like this.
So what would I add to make my tree right side up? Here is my code. Thanks in advance!
import turtle
t = turtle.Turtle()
def tree(length = 100):
if length < 10:
return
t.forward(length)
t.left(30)
tree(length *.7)
t.right(60)
tree(length * .7)
t.left(30)
t.backward(length)
return
tree()
turtle.done()
You must remember that the function is recursive, thus you need to turn the turtle outside of the function. You can use a function in a function, but I would just turn the turtle in the global scope right before you call the function:
t.left(90) # then call tree after with tree()
Related
Inside functions, I learned somewhere to work on copies of the arguments, for example: list_arg --> t = list_arg[:]. It made sense, so I try to do it consistently.
Now I am going again through the basics of programming, with the help of Think Python 2nd Edition by Allen B. Downey. In chapter 4 - Case study: interface design, I have to draw polygons with the help of the turtle module
import turtle
def mysquare(length, turtle_obj):
t = turtle_obj
t.pd()
for i in range(4):
t.fd(length)
t.lt(90)
t.pu()
def main():
bob = turtle.Turtle()
mysquare(100, bob)
turtle.mainloop()
if __name__ == "__main__":
main()
In this case, bob and t refer to the same turtle object. If I forget to make it hold the pen up t.pu() in the mysquare function, bob will (potentially) keep drawing in the main function.
As much as I would like to keep the programming style of writing functions that don't change their parameters, I don't know if it makes any sense in this situation.
Is there a general way, best practice to handle such a situation?
Conceptually, does my issue have to do with a particular programming paradigm (object-oriented, functional), or a language style (pythonic way)?
We can correct for the pen state on the way out using turtle's isdown() method:
import turtle
def mysquare(length, turtle_obj):
if was_up := not turtle_obj.isdown():
turtle_obj.pendown()
for _ in range(4):
turtle_obj.forward(length)
turtle_obj.left(90)
if was_up:
turtle_obj.penup()
turtle.penup()
mysquare(50, turtle)
turtle.goto(100, 100)
mysquare(25, turtle)
turtle.done()
The calls to mysquare() will return the pen state to what it was when the function was called. Assigning turtle_obj to t achieves nothing.
I'm very new to coding and I am attempting to create a box svg file with a fractal spiral design on one of the box wings. I'm having a difficult time with this and I'm hoping someone can offer some guidance as to where I'm going wrong.
Here is part of my code:
import turtle
def spiral(x, y):
spiral1 = turtle.setpos({},{})
return spiral1.format(x,y)
t = turtle.Turtle()
t.pensize(1)
t.pencolor('orange')
t.speed(0)
for i in range (10):
t.circle(10 + i, 45)
spiral_1 = spiral(int(box_x)*96, int(box_height)*96)
this is giving me an error:unsupported operand type(s) for * 'dict' and 'float'
Essentially what I want to do is write this spiral onto my actual svg file which has specific coordinates I've already defined (i.e box_height)). I'm not quite sure where to go from here. I'd really appreciate your help.
Edit: perhaps I need to figure out how to generate the pattern with svg code rather than turtle
I'm hoping someone can offer some guidance as to where I'm going wrong
I'm having a hard time figuring out where you went right:
spiral1 = turtle.setpos({},{})
This doesn't appear to be Python argument syntax and setpos() always returns None so there's no point in saving the result into spiral1.
return spiral1.format(x,y)
None doesn't have a format() method. Also, the return at this point in the code causes it to ignore the next six lines of code. Effectively, not drawing at all.
spiral_1 = spiral(int(box_x)*96, int(box_height)*96)
spiral() doesn't return anything useful, so no point saving the result. Let's rework your code so that it actually draws a spiral:
from turtle import Screen, Turtle
box_x, box_y = 96, 96
def spiral(x, y):
turtle.penup()
turtle.setposition(x, y)
turtle.pendown()
for i in range(100):
turtle.circle(10 + i, 45)
screen = Screen()
turtle = Turtle()
turtle.pencolor('orange')
turtle.speed('fastest')
spiral(box_x, box_y)
screen.exitonclick()
I don't see why this question is tagged [fractals]. Nor do I see why it's tagged [xml] and [svg] as it's a simple Python syntax and turtle graphics question. And you really shouldn't be getting started with Python 2.7 as it is no longer supported.
I have found this code on this website, and I have a few questions about it. I have already made a Sierpinski triangle on Python using my rudimentary knowledge and it is way too long and very bad.
I've done it using functions and some variables, but I have some questions with this code I have found. First of all, what is the "T" constantly brought up, the length and depth, and where is this all given a value. Where is the length and depth specified, and what does it do to the code?
Please note I am a beginner.
Here is the code:
import turtle
def draw_sierpinski(length,depth):
if depth==0:
for i in range(0,3):
t.fd(length)
t.left(120)
else:
draw_sierpinski(length/2,depth-1)
t.fd(length/2)
draw_sierpinski(length/2,depth-1)
t.bk(length/2)
t.left(60)
t.fd(length/2)
t.right(60)
draw_sierpinski(length/2,depth-1)
t.left(60)
t.bk(length/2)
t.right(60)
window = turtle.Screen()
t = turtle.Turtle()
draw_sierpinski(100,2)
window.exitonclick()
t = turtle.Turtle()
t is an instance of the class Turtle located in the module turtle that is previously imported
import turtle
As the instance t is in the global scope the python interpreter is able to find it, even within the function draw_sierpinski(length,depth)
I have no idea where you obtained the code however here are the docs for the turtle module.
To find out what the code does try it by yourself. Just pip install turtle and run the code
From the turtle docs
Turtle graphics is a popular way for introducing programming to kids.
It was part of the original Logo programming language developed by
Wally Feurzig and Seymour Papert in 1966. Imagine a robotic turtle
starting at (0, 0) in the x-y plane. After an import turtle, give it
the command turtle.forward(15), and it moves (on-screen!) 15 pixels in
the direction it is facing, drawing a line as it moves. Give it the
command turtle.right(25), and it rotates in-place 25 degrees
clockwise.
I'm making a recursive drawing of a tree using repl.it py turtle. This is my code
import turtle
import random
def about(x): return x * random.uniform(0.95,1.05)
# recursively draw a tree
def tree(t,a,s):
if s<2: return
t.left(a)
t.fd(s)
tree(t.clone(),about(30), s * about(.7))
tree(t,about(-30), s * about(.7))
t = turtle.getpen()
t.ht(); t.speed(0); t.tracer(0)
tree(t,90,40)
t.update()
Also here. But it only draws part of the tree. If I change it to
t.tracer(150)
then it works! Also tracer(10) works, but tracer(200) does not work. Is there a limit to how high tracer can go?
First, let's discuss your drawing code. Your tree consists of about 500 lines drawn by 500 different turtles! This seems excessive, so let's rewrite your code to use a single turtle that undoes it's movements rather clone itself:
from turtle import Screen, Turtle
from random import uniform
def about(x):
return x * uniform(0.95, 1.05)
# recursively draw a tree
def tree(t, a, s):
if s < 2:
return
t.forward(s)
t.left(a)
tree(t, about(30), s * about(0.7))
t.right(2 * a)
tree(t, about(-30), s * about(0.7))
t.left(a)
t.backward(s)
screen = Screen()
screen.tracer(0)
turtle = Turtle()
turtle.hideturtle()
turtle.setheading(90)
tree(turtle, 15, 50)
screen.tracer(1)
screen.mainloop()
As far as tracer() is concerned, I wasn't able to reproduce your results but the image never completed either. The argument to tracer() specifies that you only want to update the image on every nth graphic operation. This is very specialized and I only recommend values of 0 and 1. First, it's difficult to calculate what every nth update should be, based on the algorithm, and what makes sense visually to the user. Second, in standard Python turtle, there are some operations that cause an update regardless of tracer() setting which throws off this calculation, unless you know when these extra update occur.
In your case, for speed purposes, set tracer(0) when the intense drawing begins, and set tracer(1) when you're done drawing. Then everything should work fine.
I am having a huge issue using the turtle library. I have to write my initials AR for an assignment. Can anyone help?
import turtle
turtlescreen
turtle.pos(400,400)
turtle.forward()
Here is the code I am trying to use. I am trying to right my initial "AR" with it.
You've managed to cram three errors into four lines of code. First, you don't need this and it's an error:
turtlescreen
so toss it. Second, the pos() function returns the current turtle postion, not set it. So instead of:
turtle.pos(400,400)
You want:
turtle.setpos(400, 400)
and finally, as #Jamie notes, you need to pass a distance (in pixels) to forward(). So instead of:
turtle.forward()
Something like he suggests:
turtle.forward(15)
Complete code:
import turtle
turtle.setpos(400, 400)
turtle.forward(15)
turtle.done()
Your turtle.forward() requires an input variable in pixels. See the documentation for turtle.forward:
Move the turtle forward by the specified distance, in the direction the turtle is headed.
Try changing:
turtle.forward()
to something like:
turtle.forward(15)
There's a few mistakes I can see. Firstly, there is no command called turtlescreen so you can remove that. Next, instead of tom.pos try using tom.setpos(x, y) or tom.goto (x, y). Lastly, you should put a value in tom.forward (length). I also recommend going through the Python Turtle documentation, https://docs.python.org/3.3/library/turtle.html?highlight=turtle