Question

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

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top