3 Replies - 4113 Views - Last Post: 26 October 2012 - 04:36 AM Rate Topic: -----

#1 shamx   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-October 12

Need a little help understanding what this function does

Posted 25 October 2012 - 05:34 PM

As the topic says, I am also not sure what to input it (like I know its a list, but I'm not sure how that list is exactly formatted)

def putBlock(block):

     if (type(block)!=list):
          print ("Error: Not a list")
          return
     else: 
          print ("List Detected")
     
     
     label=block[0]
     p=[]
     for coord in block[1:]:
          p.append(coord)

     color=fetchColor(label)

     turtle.begin_fill()
     turtle.fillcolor(color)
     turtle.penup()
     
     k=p[0]
     
     
     
     turtle.setpos(k[0],k[1])
     turtle.pendown()
     for i in range(0,len(p)):
          k=p[i]
          
          if (type(k)!=tuple):
               print ("Error: Not a Tuple")
               return
          else: 
               print ("Tuple Detected")   
               
          turtle.setpos(k[0],k[1])
     k=p[0]          
     turtle.setpos(k[0],k[1])        
     
     
     turtle.end_fill()
     turtle.penup()
 
     return



Is This A Good Question/Topic? 0
  • +

Replies To: Need a little help understanding what this function does

#2 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 837
  • View blog
  • Posts: 2,271
  • Joined: 23-December 08

Re: Need a little help understanding what this function does

Posted 25 October 2012 - 07:17 PM

yuck! This is some ugly code, my friend. block should be a list where the first element is a color, and then the rest of the elements are tuples of coordinates.

For example:
block = ["purple",(44,26,),(100,100,),(77,92,)]


Notice that coordinates are always in groups of 2 (x,y).
Was This Post Helpful? 0
  • +
  • -

#3 shamx   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-October 12

Re: Need a little help understanding what this function does

Posted 25 October 2012 - 10:11 PM

View Postatraub, on 25 October 2012 - 07:17 PM, said:

yuck! This is some ugly code, my friend. block should be a list where the first element is a color, and then the rest of the elements are tuples of coordinates.

For example:
block = ["purple",(44,26,),(100,100,),(77,92,)]


Notice that coordinates are always in groups of 2 (x,y).



Thank you sooooo much, that's all I actually needed to know to figure it out ^^. Once you told me what the input was supposed to look like, it became easy :)
Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Need a little help understanding what this function does

Posted 26 October 2012 - 04:36 AM

It's poorly written though. It shouldn't be copying, or printing on every success, or returning for no reason. The absolute requirement of types is not really pythony, as it fails duck typing. The need for you to disassemble and create the block is also bad.

Don't know what fetchColor is. Don't really know why you need it.

With what you have, this would work:
from turtle import *

turtle = None

def putBlock(block):
	# orginal code here
turtle = Turtle()
putBlock(["purple",(10,10),(10,60),(60,60),(60,10)])
mainloop()




I'd instead try to be more clear about what I'm on about and more permissive in the types I'd accept. e.g.
from turtle import *

def putBlock(turtle, color, points):
	if len(points)<2:
		raise ValueError("points must have at least two values")
	for pt in points:
		if not len(pt)==2:
			raise ValueError("point must have two values")
	
	# and now you're ready for action
	turtle.penup()

	turtle.begin_fill()
	turtle.fillcolor(color)
	turtle.setpos(points[0])
	turtle.pendown()
	for pt in points[1:]:
		turtle.setpos(pt)
	turtle.setpos(points[0])
	turtle.end_fill()
	
	turtle.penup()

def main():
	turtle = Turtle()
	putBlock(turtle, "purple",([10,10],(10,60),(60,60),(60,10)))
	mainloop()
	
main()



Notice that the types of my points and each point doesn't really matter. What matters is that conform to expected behaviors. Interestingly, the setpos method also follows this, accepting a list or a set.

Most python programs won't worry about such checks. You pass something that doesn't evaluate right, you fail. No need for checks. It's your own damn fault if you can't call the method correctly.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1