Okay, went over to Python.com and loaded up this page
https://docs.python.org/3.6/library/turtle.html for Turtle Coding basics.
Right there they have a code that makes a red and yellow star
from turtle import *
color('red', 'yellow')
begin_fill()
while True:
forward(200)
left(170)
if abs(pos()) < 1:
break
end_fill()
done()
Well, I tried this code myself, and it doesn't work. I get an error message saying ExternalError: TypeError: Cannot read property 'apply' of undefined on line 2
Am I missing something? I'm running Python 3.6 I believe. I gotta say, it does not inspire a lot of confidence when something from the Python website doesn't seem to work...
UPDATE
Well, I spoke to the teachers at my Python class and apparently the "Python" program we're using is slightly different than actual Python. I'm not sure but I think they said it's emulated through Javescript.or some such. So, I had to add more basic stuff like "import turtle", name a turtle, etc.
import turtle
wn=turtle.Screen
mark=turtle.Turtle()
mark.color('red', 'yellow')
mark.begin_fill()
while True:
mark.forward(100)
mark.left(170)
if abs(mark.xpos()) < 1:
break
end_fill()
done()
When I run this program, it draws one line, turns the Turtle and says, “AttributeError: 'Turtle' object has no attribute 'xpos' on line 9”
“AttributeError: 'Turtle' object has no attribute 'xpos'
Read the turtle documentation. You have pos() method, which returns a Vec2D object.
No mention of xpos
Maybe you want xcor for x - coordinate
Related
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.
import turtle
t = turtle.Turtle()
t.forward(100)
t.done()
this code is returning an error like this
NameError: name 'turtle' is not defined
I have tried to use turtle but it doesn't seem to be working the way it is shown in the code.
I have tried referring to various sources but could not find the fix
The problem lies on this line:
t.done()
You see, you defined t as a Turtle object, and Turtle objects have no attribute done.
The done() attribute belongs to the turtle module itself, so instead of t.done(), it's turtle.done():
import turtle
t = turtle.Turtle()
t.forward(100)
turtle.done()
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.
After importing turtle, I keep trying to put t=turtle.Pen() but it says turtle doesn't have the attribute Pen.
I have tried
turtle=turtle.Pen()
t=turtle.Turtle()
myturtle=turtle.Pen()
my turtle =turtle.Turtle()
but every time it says turtle does not have attribute whatever I put as my attribute. Does anyone know why this is happening? I have python version 3.5.0
Here is a brief turtle example:
https://gist.github.com/wolfospealain/af3410a9e71eb2ff7be5625174c4f4c5
#!/usr/bin/python3.5
import turtle
turtle.shape("turtle")
turtle.left(45)
turtle.forward(50)
turtle.right(65)
turtle.circle(150,350)
turtle.home()
big=("Arial", 36, "normal")
turtle.penup()
turtle.goto(-40,150)
turtle.write("Hello World!", font=big)
turtle.home()
turtle.back(20)
turtle.exitonclick()
Notice how you must first import turtle to make the turtle library available to your program.
Notice, too, that all the method names are lower case (for example, turtle.penup()).
I think you probably meant turtle.penup(), which allows you to "move" without drawing.
You can find complete turtle documentation here: https://docs.python.org/3.3/library/turtle.html
Here is a short tutorial you might find helpful: https://www.tutorialspoint.com/turtle-programming-in-python
I am using the turtle module in python. the problem is that whenever I have the turtle move i will draw even if the pen is up. for example if I run this program:
import turtle
turtle.penup
turtle.goto(0,50)
the turtle will still draw a line when it moves to (0,50)
why is this and how can it be prevented?
It looks like you're not actually calling turtle.penup. Try this:
import turtle
turtle.penup()
turtle.goto(0,50)
You have a typo, you aren't calling the penup method:
import turtle
turtle.penup() #This needs to be a method call
turtle.goto(0,50)
import turtle
turtle.up()
turtle.goto(0,50)
turtle.down()
if you don't put the pen down it will keep on drawing in invisible condition.
This question is super old and definitely has already been answered, but I'll leave this explanation here for future people
"penup" is a method in Python, aka function in other languages. This means that when you want to use it you have it include some parenthesis just so that your code knows what is supposed to be happening
import turtle
turtle.penup()
turtle.goto(0,50)
When you don't include the parenthesis, the code thinks you are talking about a variable, and looks for one called "penup", but there is no variable of that name, so Python throws its hands up and crashes
you called penup without (). with
turtle.penup()
this will work.
Others here said that, but implicitly. trying to ensure it is clear where the typo is.
You should probably try,
turtle.penup()
no it should be something like this:
turtle.up() # This a method call
turtle.goto(0,50) # Part of the method call