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!
Related
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)
# ...
This question already has answers here:
Difference between "import X" and "from X import *"? [duplicate]
(7 answers)
Use 'import module' or 'from module import'?
(23 answers)
Closed 7 months ago.
I am trying to use a book on python, and I understand that from turtle import * imports everything into the current namespace while import turtle just brings the module in so it can be called as a class. When I try the latter, though, it breaks.
>>> import turtle
>>> t = turtle.pen()
>>> t.pen.forward(10)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
t.pen.forward(10)
AttributeError: 'dict' object has no attribute 'pen
However, using from turtle import*, assigning pen to an object and typing command forward works fine. This is not what the book says to do, but it is the only thing that works. What is happening?
If the books says something like:
import turtle
t = turtle.pen()
t.forward(10)
then it's probably a typo for:
import turtle
t = turtle.Pen()
t.forward(10)
where Pen is a synonym for Turtle -- we've seen that problem here before. (Lowercase pen() is a utility function that's rarely used except in error.)
I understand that from turtle import * imports everything into the
current namespace while import turtle just brings the module in so it
can be called as a class
My recommendation: use neither. Instead do:
from turtle import Screen, Turtle
screen = Screen()
turtle = Turtle()
turtle.forward(10)
# ...
screen.exitonclick()
The reason is that Python turtle exposes two programming interfaces, a functional one (for beginners) and an object-oriented one. (The functional interface is derived from the object-oriented one at library load time.) Using either is fine but using both at the same time leads to confusion and bugs. The above import gives access to the object-oriented interface and blocks the funcitonal one.
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
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
The code below works perfectly, however, PyCharm complains about syntax error in forward(100)
#!/usr/bin/python
from turtle import *
forward(100)
done()
Since turtle is a stanrd library I don't think that I need to do additional configuration, am I right?
The forward() function is made available for importing by specifying __all__ in the turtle module, relevant part from the source code:
_tg_turtle_functions = [..., 'forward', ...]
__all__ = (_tg_classes + _tg_screen_functions + _tg_turtle_functions +
_tg_utilities + _math_functions)
Currently, pycharm cannot see objects being listed in module's __all__ list and, therefore, marks them as an unresolved reference. There is an open issue in it's bugtracker:
Make function from method: update __all__ if existing for starred import usage
See also: Can someone explain __all__ in Python?
FYI, you can add the noinspection comment to tell Pycharm not to mark it as an unresolved reference:
from turtle import *
#noinspection PyUnresolvedReferences
forward(100)
done()
Or, disable the inspection for a specific scope.
And, of course, strictly speaking, you should follow PEP8 and avoid wildcard imports:
import turtle
turtle.forward(100)
turtle.done()
Another solution would be to explicitly create a Turtle object. Then autocomplete works just as it should and everything is a bit more explicit.
import turtle
t = turtle.Turtle()
t.left(100)
t.forward(100)
turtle.done()
or
from turtle import Turtle
t = Turtle()