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()
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()
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
I am looking at a snippet of code and I just don't understand how it works:
import pygame, sys
from pygame.locals import *
on the first line pygame is imported, and on the second line, all the methods of a subset of pygame is invoked. If the first line imports all of pygame, why do we have to specifically import a subset of the module again? Why doesn't a mere import pygame do the job in the first place?
A mere import pygame would suffice, but the author wanted to have a shorthand access to the constants of pygame. For example, instead of:
import pygame
...
resolution = pygame.locals.TIMER_RESOLUTION
it may be sometimes preferable to have
import pygame
from pygame.locals import *
...
resolution = TIMER_RESOLUTION
Note that you should still import pygame itself to be able to access to other methods/properties (other than pygame.locals.) of pygame.
The idea is that you can call all the functions in pygame.locals without using pygame.locals.someFunction, but instead someFunction.