Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Turtle Problem
#1
I'm drawing a polygon in which every side of that polygon should get a random color, that means that for every side the turtle should get a different color, this is my code:
import turtle
import random
r = 0.0


def cpolygon(n, size, colors):
	angle = 360/n 
	r = random.randint(25, 160)
	for i in range(n):
		turtle.pencolor(str(r))
		turtle.forward(100)
		turtle.left(angle)
	turtle.mainloop()

cpolygon(8, 100, r)
And this is the error:

Error:
turtle.TurtleGraphicsError: bad color string: 98
Help?
Reply
#2
https://docs.python.org/2/library/turtle...e.pencolor

if you specify pencolor as string it should be Tk string like 'yellow', 'red' or hexadecimal color like '#33cc8c'
The other option would be to specify RGB tuple
Reply
#3
Changed my code to this and it worked like magic:
import turtle
import random
colors=['blue' , 'red', 'orange', 'purple']

def cpolygon(n, size, colors):
	angle = 360/n
	for i in range(n):
		turtle.pencolor(random.choice(colors)) #chooses a random element in the given list 'colors'
		turtle.forward(100)
		turtle.left(angle)
	turtle.mainloop()

cpolygon(8, 100, colors)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Photo Problem installing turtle MasterJediKnight7 17 24,591 Mar-18-2024, 10:22 AM
Last Post: bmohamadyar313
  Begginer problem with turtle Damien_X 1 2,078 Aug-25-2020, 11:23 AM
Last Post: GOTO10
  Nested while loop problem + turtle DreamingInsanity 3 2,946 Jul-06-2019, 02:01 PM
Last Post: DreamingInsanity
  wn = turtle.screen() AttributeError: module 'turtle' has no attribute 'screen' Shadower 1 6,171 Feb-06-2019, 01:25 AM
Last Post: woooee
  Help! Turtle not working, even when we click the turtle demo in IDLE nothing happens. BertyBee 3 5,618 Jan-04-2019, 02:44 AM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020