Trying to draw Kenyan flag colours in python, encountered name error? - python

I thought trying to draw this thing in kenyan colours. Thought it would be simple and quick but ran into errors on the second line.
import turtle
pen=turtle.Turtle()
pen.speed('fastest')
When I take out the second line the error appears on the third line.
What am I doing wrong?
turtle.bgcolor('black')
col=('black','white','red','white','green')
That's what I'm trying to get to.

I've never used turtle myself, but after looking online I found this section of code which compiles:
from turtle import *
color('red', 'yellow')
begin_fill()
while True:
forward(200)
left(170)
if abs(pos()) < 1:
break
end_fill()
done()
From that I think your issue is how you are importing. Try importing turtle the same way as above, and then do:
color('black','white','red','white','green')
instead of
col=('black','white','red','white','green')
---Update-to-address-comments---
Looking at your code, let me know if I misinterpreted where white space should be.
from turtle import *
pen=turtle.Turtle()
pen.speed('fastest')
turtle.bgcolor('black')
color=('black','white','red','white','green')
for i in range(1,200,2):
t.pencolor(col[i%4])
for x in range(0,10):
t.circle(i)
t.rt(50)
turtle.done()
Your issue isn't the import it's the use of t. instead of pen. New code should look like this:
import turtle
pen=turtle.Turtle()
pen.speed('fastest')
turtle.bgcolor('black')
color=('black','white','red','white','green')
for i in range(1,200,1):
pen.pencolor(color[i%4])
for x in range(0,10):
pen.circle(i)
pen.rt(50)
turtle.done()
The colors don't seem to work though. I'll look into it a bit more in a few minutes.
note the for loop should iterate by 1 not 2 for the colors to switch properly.

Related

why is this turtle collision if statement working? can't track which turtle position is in the statement. tried to track in Pycharm, no luck

for count in range(15):
turtle.forward(random.randint(0, 100))
turtle.right(random.randint(0, 90))
turtle.forward(random.randint(0, 50))
turtle.circle(random.randint(0, 50))
if turtle.pos() == turtle.pos():
turtle.color("red")
turtle.speed(0)
#turtle.penup()
#turtle.stamp()
#turtle.hideturtle()
*this code doesnt look right but I am a beginner myself my friend insists it makes the turtle turn red on collision. But how
does
> the if statement differentiate between 1 turtle and another? they ARE
> all red at the end. How is that happening?
not sure how to use Pycharm to track which turtle is being asked about in the if statement
Thanks in Advance I am learning fast.
Do you have a second turtle? if your other turtle is called Turtle2, then it should look like this
if turtle.pos() == turtle2.pos():
turtle.color("red")
turtle.speed(0)
#turtle.penup()
#turtle.stamp()
#turtle.hideturtle()
right now you are looking to see if a turtle's position is the same as a turtle's position (the same values). It will always be true.

Why is there a second Turtle?

I am learning turtle graphics in python and for some reason there is a second turtle on the screen and I haven't even created a second turtle. How can I get rid of the second turtle?
import turtle
s = turtle.getscreen()
t = turtle.Turtle()
for i in range(4):
t.fd(100)
t.rt(90)
turtle.exitonclick()
The second turtle at the starting location appears because of the line s = turtle.getscreen().
This line is not needed (you do not use s), and if you remove it this turtle disappears but the rest of the code seems to work as before.
The turtle library exposes two interfaces, a functional one (for beginners) and an object-oriented one. You got that extra turtle because you mixed the two interfaces (and #mkrieger1's solution doesn't fix that completely).
I always recommend an import like:
from turtle import Screen, Turtle
screen = Screen()
turtle = Turtle()
for _ in range(4):
turtle.forward(100)
turtle.right(90)
screen.exitonclick()
This gives you access to the object-oriented interface and blocks the functional one. Mixing the two leads to all sorts of bugs and artifacts.
To combine the answer from mkrieger1 and cdlane, you could replace
s = turtle.getscreen()
with
s = turtle.Screen()
You've still got a variable holding the screen (in case you should ever need it), and it doesn't generate that extra turtle in the center.

How Do You Make The Size of a Circle bigger in Python Turtle

I've been making game that includes a head. I do not know how to make it bigger. Here is the code:
head = turtle.Turtle()
head.speed(0)
head.color('white')
head.shape('circle')
head.size(12)
head.penup()
See turtle.shapesize()
import turtle
head = turtle.Turtle()
head.shape('circle')
head.shapesize(12)
turtle.done()
You seem to be using the wrong function, instead of turtle.size() try using turtle.shapesize()
Make sure to use turtle.shapesize() on line 5 it wont work if you use turtle.size()

Turtle from image won't draw over another turtle's stamp

This code sets up a background of stamps placed by one turtle. Another turtle, (whose shape is from an imported image file) moves around over the background. But the second turtle is not visible whenever it is positioned over a stamp placed by the first turtle. If I make the moving turtle one of the standard shapes, eg circle, then it stays visible. So there's something odd about using an imported image for the turtle, which causes it to disappear whenever it's on top of one of the stamps.
#!/usr/bin/python3
from turtle import *
from time import sleep
scr = Screen()
scr.register_shape('player.gif')
mover = Turtle()
bgnd = Turtle()
bgnd.color('blue')
mover.shape('player.gif')
bgnd.shape('square')
for i in range(5):
bgnd.goto(i*20,0)
bgnd.stamp()
for i in range(5):
mover.goto((8-i)*20,0)
sleep(1)
The accompanying image is the one I've referred to as player.gif (I can see it at the bottom of the post, but it's very small).
Can anyone help explain why this is and how to get around it?
I'm not sure if this is a bug or subtlety, but stamps aren't pixels on the screen like dot() -- the stamp() function returns an ID that allows selectively removing them. My guess is it's a layering issue with the underlying tkinter. In turtle, if you have a layering issue, sometimes it helps to do things in a different order:
from turtle import Screen, Turtle
from time import sleep
background = Turtle()
background.hideturtle()
background.color('blue')
background.shape('square')
for i in range(5):
background.goto(i * 20, 0)
background.stamp()
screen = Screen()
screen.register_shape('player.gif')
mover = Turtle()
mover.shape('player.gif')
for i in range(5):
mover.goto((8 - i) * 20, 0)
sleep(1)
screen.exitonclick()
In the long run, time.sleep() isn't a friend of event-based turtle. It's fine for examples like this but anything more and it will cause more problems than it solves. Look into the ontimer() method of the screen.

Python Turtle Graphics Simplification

just reviewing for my upcoming midterm. We were given Past midterm problems but no solutions. I am trying to grasp the knowledge best i can.
For this problem, it asks to define a function named equalSigns, pass it values t and length. So, i just need to make my program in turtle graphics, create two parellel line, simple enough i suppose. this is my code that i wrote just for it to correctly output an equal sign of x length. (then of course i would convert it to a function) My question, is there any better way to create this?
import turtle
t=turtle.Turtle()
s=turtle.Screen()
t.forward(200)
t.penup()
t.home()
t.right(90)
t.forward(50)
t.pendown()
t.left(90)
t.forward(200)
'''i suppose i dont have to go home and then down.
instead just continue and go down and forward left.
but either way, is this the best approach to take?
'''
Yes, I think there's a better way. Most of all, I think you turned the wrong way: you need to make a second right turn to come back along the lower line.
You could make a routine that does a half-equals, and then all it twice to get the two lines. Think of this as drawing a rectangle, except that the short sides are invisible.
# Draw long side
t.pendown()
t.forward(x)
t.penup()
t.right(90)
# Move along short side without drawing
t.forward(x/4)
t.right(90)
That gets you to the opposite corner of the rectangle. Call this twice, and you're done ... and back at the starting point.
Perhaps you could have your turtle think outside the shell:
import turtle
import tkinter as _
_.ROUND = _.BUTT
turtle.width(50)
turtle.forward(200)
turtle.color("white")
turtle.width(48)
turtle.backward(200)
turtle.done()
(The vertical gray bars at the two ends are artifacts of GIF conversion and not present when the program is run.)

Categories

Resources