from turtle import *
for i in range(500): # this "for" loop will repeat these functions 500 times
forward(i)
left(91)
I want to run python code including usage of "turtle" class ,I tried to use sublime text editor after installing python environment from here : https://www.python.org/downloads/
it gives me that error
Atom:
Sublime:
I have installed atom-runner package and turtle package and still the error !
What you need to do is define what you want to move forward and turn. When you are using a module you first import it then use it's functions with the lines:
module_name.function(variables)
so you have to edit your code to make it look like this:
import turtle
for i in range(500):
turtle.forward(i)
turtle.left(91)
or
import turtle as t
and replace
'turtle'
with
't'
The problem is not in the code but the file.
You are importing your own file. You have imported turtle.py and the name of your file is turtle.py.
Never name your .py file the same as a module. Rename your file to a name that is not a module in your standard library.
The problem in sublime in sublime is that you have not have not configured it to run Python files correctly.
you are mostly good to go.
#1 always import turtle. [Looks good]
Code: import turtle
#2 Write out your handle.[Looks good]
Code: jojo = turtle.Turtle()
#3 Completion of your turtle should be closed properly, otherwise your program will close
on yourself. [I think this was missing.]
code: turtle.done()
example: This should work (atom editor)
import turtle
jojo = turtle.Turtle()
jojo.color("red")
jojo.pensize(5)
jojo.shape("turtle")
jojo.forward(100)
jojo.left(90)
jojo.forward(100)
jojo.left(90)
jojo.forward(100)
jojo.left(90)
jojo.forward(100)
turtle.done()
example: This will close on itself (atom editor)
import turtle
jojo = turtle.Turtle()
jojo.color("red")
jojo.pensize(5)
jojo.shape("turtle")
jojo.forward(100)
jojo.left(90)
jojo.forward(100)
jojo.left(90)
jojo.forward(100)
jojo.left(90)
jojo.forward(100)
#turtle.done()
Related
I'm trying to make a turtle game on Repl.it and I don't know why this error keeps coming up. Here is my code:
import turtle
wn = turtle.Screen()
wn.bgcolor("white")
wn.setup(width=800, height=800)
wn.tracer(0)
# Set up the ground
ground = turtle.Turtle()
ground.color("white")
ground.shape("square")
ground.speed(0)
ground.shapesize(stretch_wid=200, stretch_len=20)
ground.speed(0)
ground.color("black")
ground.penup()
ground.goto(0, -400)
ground.direction = "stop"
It seems that the implementation of the turtle library on repl.it is somewhat limited and not all commands are available. You can either run it locally to use all commands or remove incompatible commands.
Source: https://repl.it/talk/ask/Turtle-python-resizing/7312
There is actually a way to do this.
Whenever you create a new repl instead of creating a "python with turtle" repl just create a normal python one. Then import the turtle module and it should work. When importing it on a basic python file it imports everything.
Whenever you do import turtle it is also a good idea to add from turtle import * that way you import everything.
I also had this problem
You can use Python with pygame instead of python with turtle because for some reason, it works
I'm new to the Python turtle library and I have a problem that confuses me a lot.
I can work with turtle in real time, but when I write a program and I save it to a file, I can't run it.
The code I have written follows below:
from turtle import *
turtle.Pen(9999999)
penup()
for i in range(16):
write(i,align='center')
forward(25)
goto(0,-5)
x=0
right(90)
for i in range(16):
pendown()
forward(400)
penup()
x+=25
goto(x,-5)
but it didn't work at all.
It gave me this error:
Traceback (most recent call last):
File "C:\Users\Nobody\Desktop\main.py", line 3, in <module>
turtle.Pen(9999999)
NameError: name 'turtle' is not defined
I think it doesn't import turtle at all.
You named your own file turtle.py
So your main.py is importing your own turtle.py, not python's turtle module.
Delete turtle.py from your desktop (and the turtle.pyc that was automatically generated).
from turtle import *
this line imports everything to the default module namespace, so you don't have to add turtle. prefix to anything
Instead of turtle.Pen you want just Pen
from turtle import *
Means literally: import all from the file turtle.py.
Python interpreter firstly checks in the current directory for any match with turtle.py and if not found anything, it will search inside library folders.
In your case, you are importing all classes, all functions and all global variables (at least not private ones) from turtle.py so you need to use
Pen(9999999)
Instead of
turtle.Pen(9999999)
Here's a rework of your code that runs fine for me:
from turtle import *
speed('fastest')
penup()
for i in range(16):
write(i, align='center')
forward(25)
goto(0, -5)
right(90)
x = 0
for i in range(16):
pendown()
forward(400)
penup()
x += 25
goto(x, -5)
hideturtle()
done()
If it runs for you, great. If it doesn't, let us know the complete error you're getting as a comment follow on to this answer. Make sure you don't have a personal turtle.py file, as #nosklo notes.
My advice is when it comes to invoking library functions, it's better to lookup rather than make up.
I'm trying to use Turtle, but for some reason, it won't let me import it. I've tried from turtle import * (and that works) but if I try print(dir(turtle)) or to use any functions, I get an error saying turtle is not defined.
from turtle import Turtle doesn't work but print(dir(Turtle)) after using from turtle import * does work. However, prefixing commands with Turtle. ie Turtle.color("red") doesn't work.
Also, the demonstrations in the turtledemo folder work. I'd really appreciate any help
This imports turtle into the main namespace and is used as follows:
import turtle
turtle.something()
Thus, you now have an identifier in your main namespace, named turtle.
This imports all visible identifiers from turtle into the main namespace
and is used differently:
from turtle import *
something()
In this scenario, turtle is not in the main namespace. Its contents
are. Thus, dir(turtle) will fail, because that identifier isn't there.
Are you sure that from turtle import Turtle doesn't work? It worked
for me.
If you import all from turtle then you have to use the Turtle object directly, like this:
from turtle import *
print(dir(Turtle))
and not like this:
from turtle import *
print(dir(turtle))
I have two pictures "gif" and want to insert them as small pictures and add them to Python code. I want the picture to stay in an accurate location as well decrease it size. When i try to run this code the shell shows an error "screen isn't defined"
import turtle
import time
from tkinter import *
screen=turtle.Turtle()
screen=turtle.getscreen()
screen.register_shape("health.gif")
screen.penup()
screen.shape("health.gif")
screen.goto(x+50,y+150)
I don't know why you're getting that error... but you have a mixup with your variable names... you create a variable you call screen that is turtle.Turtle() but then you overwrite that variable by doing turtle.getscreen()
Doing:
import turtle
t=turtle.Turtle()
screen=t.getscreen()
screen.register_shape("health.gif")
t.penup()
t.shape("health.gif")
Draws a health.gif image (if that happens to be in your working directory)
IMPORTANT i did not notice that there were two different places where i had to change the settings from Qt4 to SVG. I changed them both and the problem regarding the "no Turtle found" was solved. I need to thank Jonathan March, who turned me in the right direction with the link he suggested.
PROBLEM SOVED!!
I'm using Canopy 1.3.0.1715 (32bit) on a MacBook Pro 64bit OS 10.9.2.
When i try to use
from turtle import Turtle
Canopy says
name 'Turtle' is not defined
Here is my code, named draw.py (i want to draw a square):
from turtle import Turtle
t = Turtle()
def drawsquare(t, x, y, side):
t.up()
t.goto(x,y)
t.setheading(270)
t.down()
for count in range(4):
t.forward(side)
t.left(90)
I also created a file turtle.cfg like this
width = 300
height = 200
using_IDLE = True
colormode = 255
Please be as simple as you can, i just recently started using Python. Thanks everyone.
from turtle import Turtle
works for me on Canopy-32 bit on Mac64 (running in the Canopy python shell).
First thing to check: did you name some file turtle.py? If so, rename the file, delete the file turtle.pyc in the same directory if it exists, and try again. (If you name your file turtle.py, then python has no way to find the standard turtle module.)
Otherwise:
Where are you running this? In the Canopy python (ipython) shell?
Or did you start up Python some other way?
Wherever it is, what do you see when you type this?:
import sys, turtle
print sys.prefix
print turtle.__file__
Also, while this should not account for your import failure, be sure to read and follow the following:
https://support.enthought.com/entries/21793229-Using-Tkinter-Turtle-in-Canopy-s-IPython-panel