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.
Related
In korean python textbook for highschool
to use turtle lib with the fastest pace user can use two verson of command.
import turtle as t
t.speed(0)
t.speed=0
is there a difference with two command?
I try to ask my teacher because of performance evaluation but.. unfortunately she didnt knowㅜㅠ..
t.speed = 0 overwrites the module function (or Turtle() method if you'd made an instance) with an integer 0:
>>> import turtle
>>> turtle.speed = 0
>>> turtle.speed("fastest")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
In general, all turtle API setters are function/method calls, not assignments. For example, turtle.setheading(90) and turtle.pencolor("red") set the heading to 90 degrees and the pen color to red, respectively.
If you want the turtle speed to be instantaneous, try turtle.tracer(0) and run turtle.update() to trigger a rerender after you've done your drawing.
As an aside, please don't use import turtle as t (although that's better than from turtle import *). It's unclear that t is the module and not a turtle instance, which makes ownership of the module confusing. Prefer making an instance of the turtle rather than calling global turtle functions, for example, t = turtle.Turtle().
t.speed=0 doesn't actually work (as ggorlen pointed out). If you want your trutle drawing to go really fast, you can make them happend off screen and only show the result at the end using screen(), tracer() and update():
Try this without the extra lines, then again after uncommenting the 3 lines:
import turtle as tg
def spiral(N):
tg.clear()
d = 1
for i in range(N):
tg.forward(d)
tg.left(20)
d+=0.3
## wn = tg.Screen() # Remove the 3 comments to make it fast
## wn.tracer(0)
tg.speed(0)
spiral(1000)
## wn.update()
I'm making a simple snake game in python. I am in the earlier stages of just making the snake move at this point. So I have 3 files, main.py, Turtle_Skin.py and Turtle_Control.py
The first part (In Turtle_Skin.py) is working just fine where I need to make the snake take the starting position, however even if I try migrating the code from Turtle_Control.py to main.py (to make sure it executes and doesn't get left behind while importing), it won't execute
My Code with file names:
main.py:
from Turtle_Control import *
from Turtle_Skin import *
positions_goto()
Turtle_Skin.py:
from turtle import *
screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("Snake_Food_Game")
screen.tracer(1)
Baby_Turtle = Turtle()
Mommy_Turtle = Turtle()
Daddy_Turtle = Turtle()
All_Turtles = [Baby_Turtle, Mommy_Turtle, Daddy_Turtle]
for turtle in All_Turtles:
turtle.shape("square")
turtle.pencolor("white")
turtle.color("white")
def positions_goto():
Daddy_Turtle.penup()
Daddy_Turtle.goto(x=-40, y=0)
Mommy_Turtle.penup()
Mommy_Turtle.goto(x=-20, y=0)
Baby_Turtle.penup()
positions_goto()
screen.exitonclick()
Turtle_Control.py
from Turtle_Skin import *
import time
positions_goto()
is_on = True
while is_on:
screen.update()
time.sleep(0.1)
for part_num in range(len(All_Turtles) - 1, 0, -1):
xcord = All_Turtles[part_num - 1].xcor()
ycord = All_Turtles[part_num - 1].ycor()
All_Turtles[part_num].goto(x=xcord, y=ycord)
Baby_Turtle.forward(20)
screen.exitonclick() blocks your code, running the main turtle loop, until you click the screen.
Tracing the code execution:
main.py runs from Turtle_Control import * on line 1
Turtle_Control.py runs from Turtle_Skin import * on line 1
Turtle_Skin.py runs most of the turtle code, then blocks at screen.exitonclick(). After you click, only then does from Turtle_Skin import * on line 1 of Turtle_Control.py resolve so that line 2, import time, can continue. But by then the window's been destroyed so the while loop is much too late.
A good way to figure out what's going on with this behavior is to add print()s to your code to see if the code you care about is even executing, and if so, when. Creating a minimal example of the problem would make the issue obvious:
import turtle
turtle.exitonclick() # => blocks until the screen is clicked
print("hi") # => only executes after the screen was clicked
The original code organization doesn't make much sense. Modules have no obvious responsibility. positions_goto() is called in many different locations. The main code that initializes turtles and runs the game loop is spread across a few files seemingly haphazardly.
With such a small amount of code, creating modules seems premature here. I'd put all of the code into one file until you have things working ("I am in the earlier stages of just making the snake move at this point") and really need obvious separation of concerns. When you do, I'd create different files for different classes (things/entities in the game), primarily. snake.py with class Snake: would be one example. food.py with class Food: might be another potential file.
There should be no "loose" code in the global scope in each file other than a class or function or two. Main-line code (particularly if non-idempotent) in modules should be in an if __name__ == "__main__": block so that it's not invoked simply because the module was imported (which might happen multiple times in an app, as is the case here).
If you want to separate the whole game from main, that's fine, but keep the set up and main loop intact so they execute as a unit.
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 working on a python project and I'm using VS Code.
Any python file I write and include turtle
from turtle import *
up()
goto(-200, k)
down()
I get errors like:
Undefined variable 'goto'
Undefined variable 'down'
Undefined variable 'forward'
Undefined variable 'left'
I get an error like this for every time I use a turtle element (I got like 100+ total)
The code does work so this isn't crucial, but it just bothers me a bit cause it's just there, you know.
Try,
from turtle import *
t = Turtle()
t.up()
# ... etc.
You need to create a instance.
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