So i made a python turtle thing a while ago and it worked just fine. However, when i try to run it now it doesnt work. Here is the code:
import turtle
turtle.forward(100)
turtle.right(144)
turtle.forward(100)
turtle.right(144)
turtle.forward(100)
turtle.right(144)
turtle.forward(100)
turtle.right(144)
turtle.forward(100)
turtle.exitonclick()
It gives a error saying:
Exception has occurred: AttributeError
module 'turtle' has no attribute 'forward'
File "C:\Users\melek büyük\Desktop\Programlama\Python\ptrh.py", line 2, in
turtle.forward(100)"
It didn't do this before. Can someone please help me?
Seems like your file might be called turtle or you have another file called turtle. This would cause the import to not actual import the turtle library, so you should rename the file.
I guess you forgot to initialize a new object like this:
import turtle
your_turtle = turtle.Turtle()
# And then you can start drawing
your_turtle.forward(100)
your_turtle.right(144)
your_turtle.forward(100)
your_turtle.right(144)
your_turtle.forward(100)
# ...
Related
This question already has answers here:
I get a syntax error about File "<stdin>", line 1
(2 answers)
Closed 1 year ago.
I use Python, and for a school project, I needed to import Turtle for a drawing program. At the start of my program, I added:
from turtle import *
When I ran the finished program, an syntax error popped up in the terminal/shell.
picture of terminal
I tried reiterating my code, from using the "import turtle" to aliasing, but nothing worked. What could possibly be happening and how do I fix it?
When importing a module in python, you can import using import turtle and you need to specify the module before each function like
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
If you import turtle using from turtle import *, you only need to call the functions from the module, like:
forward(100)
left(90)
forward(100)
You can also do: import turtle as t and do:
t.forward(100)
t.left(90)
t.forward(100)
This happened to me on VS Code too, it happens when you run the program using the green play button .Try using f5 to start your program and it should work. Hope this works!
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()
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
This is my code :
import turtle
wn = turtle.Screen()
alex = turtle.Turtle()
alex.forward(50)
alex.left(90)
alex.forward(30)
wn.mainloop()
And my error is: "_Screen object has no attribute mainloop"
I tried to do just "mainloop" but then I got NameError while doing alex.mainloop gave same error i.e. turtle object has no attribute mainloop.
Googling told me that I might have my file name as turtle.py creating the conflict but that's not the case.
You just need to call the mainloop() function on the turtle module. So your last line should be:
turtle.mainloop()
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