turtle.setx() function in Python
The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses tkinter for the underlying graphics, it needs a version of Python installed with Tk support.
turtle.setx()
This method is used to set the turtle’s first coordinate to x, leave the second coordinate unchanged. Here, whatever the position of the turtle is, it set the x coordinate to given input keeping y coordinate unchanged.
Syntax : turtle.setx(x)
Parameter:
x: a number (integer or float), it is the only one required argument.
Below is the implementation of the above method with some examples :
Example 1 :
Python3
# import package import turtle # check the turtle position print (turtle.position()) # set the x coordinate turtle.setx( 30 ) # check the turtle position print (turtle.position()) # set the x coordinate turtle.setx( - 50 ) # check the turtle position print (turtle.position()) |
Output :
(0.0, 0.0) (30.0, 0.0) (-50.0, 0.0)
Example 2 :
Python3
# import package import turtle # set turtle direction turtle.left( 90 ) # loop for pattern for i in range ( 4 ): # motion turtle.forward( 100 ) turtle.right( 90 ) turtle.forward( 20 ) turtle.right( 90 ) turtle.forward( 100 ) # set the x coordinate turtle.up() turtle.setx( 40 * (i + 1 )) turtle.down() # change the direction turtle.left( 180 ) |
Output :
Contact Us