Hexspiral code issue

#NiceHexSpiral.py
import turtle
colours=[‘red’,‘purple’,‘blue’,‘green’,‘yellow’,‘orange’]
t=turtle.pen ()
turtle.bgcolor (‘black’)
for x in range (360):
t.pencolor (colors[x%6])
t.forward(x)
t.left (59)
The above code is problem.when I try to run.I don’t understand. Please help

Ok first of all, your list of colors is “colours” and not “color” when you use it. Notice you put a “u” in the name. Second, pen returns a dictionary of values about the pen. You draw with “turtle” not “pen” like you do when set “t” to the pen. Functions like pencolor, forward and left are done with the turtle module itself. Below is your program with these changes put in place…

import turtle
colours=['red','purple','blue','green','yellow','orange']

turtle.bgcolor('black')

for x in range(360):
	turtle.pencolor(colours[x%6])
	turtle.forward(x)
	turtle.left(59)

As you can see from the code, we made sure that we used the right name of the colour list.Second we are using methods from the “turtle” module. We change the pen color, then we move the turtle down and left while in the range.

This program makes a multicolored hex shape that is nice. :slight_smile:

2 Likes

Thank you for reply.I am start learning a book call"tech your kids to code"by Bryson Payne.those code found in.sir I am currently using "qpython"and your codes give an error.please check it

It would be helpful if you told us what the error message said! :slight_smile:

@Martyr2’s code works fine for me using the official version of Python on Windows.

2 Likes

I am using "qpython " it’s showing an indentation is missing.where ?

This is a different script from the one you originally showed and the image you’ve posted doesn’t show the error you say is reported.

1 Like

I update the screen shot.Please take a look

you sure you’ve copied the code correctly? look again…

2 Likes

Again the screenshot is trying to use “t” which is the returned dictionary from Pen(). This is not correct. Please look at the code I have shown you where you use “turtle” instead. I encourage you cut and paste my code into your program (making sure to ident as required) and try that. This code works and has been tested by me.

:slight_smile:

1 Like

alternatively: t = turtle.Turtle() instead of .Pen(), which will then return the turtle object, and the rest of the code works as-is.

1 Like

I try everything but not working in "qpython "app.Attribute issue in line 7

I think you’ll need to post your current code and tell us which is line seven if you want anyone to help with that. smile

1 Like

#NiceHexSpiral.py
import turtle
colours=[‘red’,‘purple’,‘blue’,‘green’,‘yellow’,‘orange’]
t=turtle.pen ()
turtle.bgcolor (‘black’)
for x in range (360):
t.pencolor (colors[‘x%6’]): this
t.width (x/100+x)
t.forward(x)
t.left (59)

I may be missing something, but that just looks like the same code you posted in your OP. Did you not try the solutions which have already been offered to you?

1 Like

There are differences in the code. But why are you posting it? Does it ork? Doesn’t it work? If it doesn’t work, what is the result you are getting that you don’t expect?

2 Likes

I try to run above code on "qpython ",but i face these issue in screenshot

I get the feeling that you are not listening to what the people here are telling you. Lets review…

  1. Your list is named “colours” yet you use “colors” which is missing the “u”
  2. Next, turtle.Pen is not the same as turtle.pen. Python is case sensitive!
  3. You draw using turtle module directly or, as pointed out, turtle.Turtle(). This means your reference to pen is just to get the pen details, not for drawing.

Again, I suggest you go back to my fully working demo program, try that out and get it to work, then you can customize it little bit by little bit until you get it working how you want it.

3 Likes

I do follow all instructions but I am faild.attribute issue

So who’s instructions did you follow,

Martyr’s in post #2 when he told you to replace the references to t with the ones for turtle, or mine in post #10 when I told you you could replace the reference to Pen with one to Turtle?

1 Like

"New problem.
Module name not found "

from turtle import*

turtle.pensize (2)
turtle.bgcolour('skyblue')
turtle.speed (0)

for i in range  (6):
    colours=['red',
'purple','blue','green','yellow',
'orange']

    turtle.color (colours)
    turtle.circle  (100)
    turtle.left (10)
    
turtle.hideturtle ()


 
for x in range (360):
     
	turtle.Turtle (colours[x%6])
	
	turtle.forward(x)
	
	turtle.left(59)