4

I am having trouble trying to finish a python turtle program. When I attempt to input x and y coordinates as well as a radius value for the function like t.drawSnowman(x = 25,y = 25,radius = 25) the program misbehaves when I input values.but if I leave out the parameters above and instead just use t.drawSnowman() the program works as intended but I am not able to create various instances of the snowman.

I would really like help trying to figure out how to input the parameters and still have the program function.

here's my code

import turtle

class MyTurtle(turtle.Turtle):
""""""


def __init__(self):
    """Turtle Constructor"""
    turtle.Turtle.__init__(self, shape="turtle")

def drawNose(self, x=0, y=0, radius = 15, circle_color = "black"):
    '''draw nose '''
    self.pendown()
    self.goto(-(radius) * (0.25),(radius * 6)+(radius * 4)+(radius))
    self.goto(0,(radius * 6)+(radius * 4)+(radius)+(radius * (0.25)))
    self.goto((radius) * (0.25),(radius * 6)+(radius * 4)+(radius))
    self.goto(0,(radius * 6)+(radius * 4)+(radius))
    self.penup()

def leftEye(self, x=0, y=0, radius = 15, circle_color = "black"):
    '''draw left eye'''
    self.pendown()
    self.circle(radius*(.25))
    self.penup()
def rightEye(self, x=0, y=0, radius = 15, circle_color = "black"):
    '''draw right eye'''
    self.pendown()
    self.circle(radius*(.25))
    self.penup()
def bottomOfHat(self, x=0, y=0, radius = 15, circle_color = "black"):
    ''' draw the long part of the hat at the bottom '''
    self.goto(0,(radius * 6)+(radius * 4)+(radius * 2))
    self.pendown()
    self.goto(-(radius),(radius * 6)+(radius * 4)+(radius * 2))
    self.goto(-(radius),(radius * 6)+(radius * 4)+(radius * 2)+(radius * (0.5)))
    self.goto(radius,(radius * 6)+(radius * 4)+(radius * 2)+(radius * (0.5)))
    self.goto(radius,(radius * 6)+(radius * 4)+(radius * 2))
    self.goto(0,(radius * 6)+(radius * 4)+(radius * 2))
    self.penup()

def topOfHat(self, x=0, y=0, radius = 15, circle_color = "black"):
    '''draw the top bigger part of the hat'''
    self.goto(radius*(.75),(radius * 6)+(radius * 4)+(radius * 2)+(radius * (0.5)))
    self.pendown()
    self.goto(radius*(.75),(radius * 6)+(radius * 4)+(radius * 2)+(radius * 2))
    self.goto(-(radius)*(.75),(radius * 6)+(radius * 4)+(radius * 2)+(radius * 2))
    self.goto(-(radius)*(.75),(radius * 6)+(radius * 4)+(radius * 2)+(radius * (0.5)))

def bottomCircle(self, x=0, y=0, radius = 15, circle_color = "black"):
    '''draws the bottom circle'''
    self.pendown()
    self.circle(radius * 3)
    self.penup()
def middleCircle(self, x=0, y=0, radius = 15, circle_color = "black"):
    '''draw the middle circle'''
    self.pendown()
    self.circle(radius * 2)
    self.penup()
def topCircle(self, x=0, y=0, radius = 15, circle_color = "black"):
    '''draw the top circle'''
    self.pendown()
    self.circle(radius)
    self.penup()
def drawSnowman(self, x=0, y=0, radius = 15, circle_color = "black"):
    self.bottomCircle()
    self.goto(0,radius * 6)
    self.middleCircle()
    self.goto(0,(radius * 6)+(radius * 4))
    self.topCircle()
    self.goto(-(radius) * (0.5),(radius * 6)+(radius * 4)+(radius))
    self.leftEye()
    self.goto((radius) * (0.5),(radius * 6)+(radius * 4)+(radius))
    self.rightEye()
    self.goto(0,(radius * 6)+(radius * 4)+(radius))
    self.drawNose()
    self.bottomOfHat()
    self.topOfHat()



t = MyTurtle()
t.hideturtle()
radius = 15
t.drawSnowman(x = 25,y = 25,radius = 25)

this is a picture of the snowman when I use the parameters t.drawsnowman(x = 25 y=25 radius = 25) messed up snowman

this is the snowman when I inuput no parameters t.drawsnowman() correct snowman

John Anthony
  • 77
  • 2
  • 8
  • 2
    you are passing x and y arguments to drawSnowman, but you are not using them inside. I can see that you only use the radius. And what do you mean by drawing does not work? – Mihai Zamfir Mar 31 '14 at 09:16
  • You should post compilation output so we can see how in what way it's not working. And I assume you're using this: https://docs.python.org/2/library/turtle.html (?) – powlo Mar 31 '14 at 10:35
  • Yes i am using turtle graphics so there is not any compilation output because the desired result is a drawn picture, which in this case is a snowman. by drawing does not work I meant that the when I run the program I get different parts of the snowman across the screen. Instead of the three snowballs being together I get 20 pixels of space in between each snowball. – John Anthony Mar 31 '14 at 13:36
  • can you explain why the x and y arguments are not being used inside, I dont understand why they are not. – John Anthony Mar 31 '14 at 13:54

1 Answers1

2

Try going step-by-step and work out what your code actually does (or use a debugger).

  • If you call t.drawSnowman(), radius will 15.
  • You call bottomCircle() which will draw a circle with a radius of radius * 3 = 45
  • You move radius * 6 = 90 on the y axis (you're now "on top" of the circle)
  • You call middleCircle() which will a circle with a radius of radius * 2 = 30
  • ...

Now, see what happens if you call your function with parameters:

  • If you call t.drawSnowman(x = 25,y = 25,radius = 25), radius will 25.
  • You call bottomCircle() which will draw a circle with a radius of radius * 3 = 45. Note how the local variable radius is 15, since it's the default value of that parameter, and you don't pass the value you passed to drawSnowman down to the bottomCircle method.
  • You move radius * 6 = 150 on the y axis (you're now far off of the circle)
  • You call middleCircle() which will a circle with a radius of radius * 2 = 30. Note how the local variable radius is 15, too.
  • ...

So, you problem is that you pass in a value for radius, but you use that value only for moving the turtle in drawSnowman, not further down in any of the other functions.

sloth
  • 99,095
  • 21
  • 171
  • 219
  • Thank you, your answer was perfect in helping me finish the program. I was caught up looking at the drawing pieces and it distracted me from actually looking at my code to see what was really going on. – John Anthony Mar 31 '14 at 18:34