May I get an explanation about the turtle module command:
.goto(0, 0)
I know that this command moves the turtle but I'm trying to find out what these arguments inside the parentheses exactly mean, in details?
Thanks.. your help is much appreciated.
I believe that the first parameter is the x-coordinate and the second parameter is the y-coordinate. So, calling this function will move the turtle to whatever (x, y) point that you give it.
https://docs.python.org/3/library/turtle.html?highlight=goto#turtle.goto
Related
I am having a huge issue using the turtle library. I have to write my initials AR for an assignment. Can anyone help?
import turtle
turtlescreen
turtle.pos(400,400)
turtle.forward()
Here is the code I am trying to use. I am trying to right my initial "AR" with it.
You've managed to cram three errors into four lines of code. First, you don't need this and it's an error:
turtlescreen
so toss it. Second, the pos() function returns the current turtle postion, not set it. So instead of:
turtle.pos(400,400)
You want:
turtle.setpos(400, 400)
and finally, as #Jamie notes, you need to pass a distance (in pixels) to forward(). So instead of:
turtle.forward()
Something like he suggests:
turtle.forward(15)
Complete code:
import turtle
turtle.setpos(400, 400)
turtle.forward(15)
turtle.done()
Your turtle.forward() requires an input variable in pixels. See the documentation for turtle.forward:
Move the turtle forward by the specified distance, in the direction the turtle is headed.
Try changing:
turtle.forward()
to something like:
turtle.forward(15)
There's a few mistakes I can see. Firstly, there is no command called turtlescreen so you can remove that. Next, instead of tom.pos try using tom.setpos(x, y) or tom.goto (x, y). Lastly, you should put a value in tom.forward (length). I also recommend going through the Python Turtle documentation, https://docs.python.org/3.3/library/turtle.html?highlight=turtle
Currently I am trying to use commands to tell a turtle to draw the characters "S," "3," and "5" using the turtle module - without using the command turtle.write. Currently my code looks like:
import turtle
def halfcircle(parts=360, line=1, direction=1):
for x in range(parts//2):
turtle.forward(line)
turtle.left(360/parts * direction)
turtle.tracer(False)
for x in range(2):
halfcircle(20, 360/30, 1)
halfcircle(20, 360/30, -1)
turtle.update()
But the loop is never ending. Additionally, I am unable to incorporate linear and nonlinear lines into one character in order to draw a 5 or 3. Any help would be much appreciated.
Before I could actually check this out, I had to add an extra line of turtle.exitonclick() to the script to prevent it from disappearing from my screen as soon as I ran it.
From what I'm seeing, it doesn't appear to actually be never ending. The turtle draws four half-circle shapes then stops. When you say never ending, do you mean that it's repeating more than you wanted/expected? Or are you calling this code somewhere else and it's really never stopping in some other part?
If you just mean that it's drawing too many half-circles (4, when you want 2 to make an S), that's because of the 2 in your for-loop for x in range(2): that for-loop has two half-circles in it, so when the loop runs twice (because of range(2)), you get 2 * 2 = 4 half-circles. [BTW, as a convention, we generally use a single underscore _ to indicate "throw-away" variables. You'll see that the var x does not actually get used in your for-loop, it's just necessary to state some var for the for-loop syntax. A lot of Python programmers just write for _ in range(2) as a way of saying, "Don't pay attention to this var, it's not going to get used."]
So if you change the for-loop from range(2) to being range(1), you get a single pair of half-circles and the program halts on this image:
Which, admittedly, is not a great S shape, but it's getting there. In general, drawing symbols is hard. I think a really good S is probably the hardest one. I think the easiest might be a 5? The best approach to figuring out how to code symbols is to draw it very large and very carefully on graph paper, then break it down into its component parts. Let's use 5 as an example, working from bottom to top:
A curved section, maybe half-circle, maybe more like 2/3's of a circle, maybe more of an ellipse, kinda hard to be certain.
A straight line going (mostly) up (or maybe just a hair to the right)
A straight line going directly right, a little longer than the previous line
Then, with that series of motions in mind, create code to draw each piece on its own. Once you've done that, just have the turtle draw the first piece, then immediately draw the next, then the next. You'll have to figure out how to reset the turtle's direction between pieces, but other than that, you'll have a 5! It's not so much that you want to have nonlinear and linear segments together, as a single symbol is created by drawing one segment after another, and you just write code that draws the specific segment one at a time.
Trying to make a simple drawing program based on x an y coordinates an i'm using a function to draw and modify the coordinates in one call without actually giving valuea to the function itself using global variables for what needs modification by the function but it still seees as if i've given it actual direct input.
In a previous version i got away by using a class to memorize the ghanging coordinates and functions of the respective class to do the drawing, but in this case the input method is slightly different since i'm using the scale widget isntead of the buttons and as i mentioned before i did try using global variables in both programs actually and it doesn't work in either of them.
from tkinter import *
win=Tk()
win.title("Etch a Schetch")
win.configure(background="grey")
win.configure(bd=5)
global xa
xa=0
global ya
ya=0
def MOVE():
tabla.create_line(xa,ya,sx.get(),sy.get())
xa=sx.get()
ya=sy.get()
tabla=Canvas(win,width=300,height=300)
sx=Scale(win,from_=0,to=300,length=300,orient="horizontal",command=MOVE)
sy=Scale(win,from_=0,to=300,length=300,command=MOVE)
ex=Button(win,text="Exit",command=exit)
tabla.grid(row=1,column=1)
sx.grid(row=2,column=1)
sy.grid(row=1,column=2)
ex.grid(row=2,column=2)
win.mainloop()
If it would work it would be kinda like an etch a sketch.
I actually just realized what the problem was, to quote mkiever who commented first but i didn't understand untill i did some individuall testing on the interaction between "Scale" and the command that is calling. To put it short and easier to understand the function that is beeing called by "Scale" as its command automaticly takes the value of the scale as an input to the function, as a rezult i only need one declared variable in the paranthesis when i define the function but no paranthesis or input variable is required to give an input to it from the scale.
EXAMPLE:
from tkinter import *
import time
win=Tk()
def P(a):
print(a)
sx=Scale(win,from_=0,to=300,length=300,orient="horizontal",command=P)
sx.pack()
win.mainloop()
Some alteration to the code is still required but it'll be much easier without that pesky error showing up.
Thank you everyone for your advice.
I'm creating a function that will cause a giraffe to move in a certain direction and distance (don't ask).
If the user types "west" in the parameters, the turtle should move west however the direction is. But the turtle's angle changes every time, I can't fix a value for my turtle to turn to go a certain direction.
I need to set the angle to, say, 90 so that it can move East. I can't find useful functions for this purpose. I tried to use Pen().degrees(90) but that didn't work. Does anyone know how I can do this?
You're looking for turtle.setheading()
>>> turtle.setheading(90)
>>> turtle.heading()
90.0
You can combine this with a simple dict to get exactly the result you are after.
turtle.right(90)
this will turn the turtle 90
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