bob = Turtle() 
bob.shape("turtle")


class SimpleDraw(Turtle):
   def __init__(Turtle):
        bob = Turtle
   def draw_square(bob, length):
       '''This function draws a regular square'''
       for i in range(4):
           bob.forward(length)
           bob.left(90)


   def draw_polygon(bob, n, length):
       '''This function draws a regular polygon'''
       angle = 360.0 / n
       for i in range(n):
           bob.forward(length)
           bob.left(angle)


   def draw_circle (t, r):

      circumference = 2 * math.pi * r
      n = 50
      length = circumference / n
      polygon (t, n, length)    


drawObj = SimpleDraw

drawObj.draw_polygon   
drawObj.draw_circle 
drawObj.draw_square


def testSimpleDraw():
   number = raw_input('Please choose a draw option: \
   1-draw square \
   2-draw polygon \
   3-draw circle \
   0-to exit');
   if number == 0:
       exit()
   else:
       if number == 1: 
           drawObj.draw_square(bob, 5)

       else:
           if number == 2:
               drawObj.draw_polygon(bob, 7, 70)

           else:
               if number == 3: 
                   drawObj.draw_circle(50, 50)


if __name__ == '__main__':
   testSimpleDraw()

没有正确的解决方案

其他提示

drawObj = SimpleDraw needs to be changed to drawObj = SimpleDraw()

That's how you instantiate a class. You are assigning the SimpleDraw class to drawObj - drawObj is the same as SimpleDraw class not an instance of SimpleDraw in your code.

In this line:

drawObj = SimpleDraw

you assign the class SimpleDraw to the name drawObj. Instead, you should create an instance of the class to assign:

drawObj = SimpleDraw() # note parentheses

You don't need the __init__ in your SimpleDraw class; you inherit this from Turtle. It is conventional to call the first argument to instance methods self, rather than bob! For example:

def draw_square(self, length):
    '''This function draws a regular square'''
    for i in range(4):
        self.forward(length)
        self.left(90)

You will need to adjust your draw_circle method to correctly call the draw_polygon method:

def draw_circle(self, r, n=50):
    circumference = 2 * math.pi * r
    length = circumference / n
    self.draw_polygon(n, length)

Note that you can use elif to simplify your code:

if number == 0:
    ...
elif number == 1:
    ...

I'm going to take a guess at what you're trying to do. First, lets rewrite this code and I'll explain the changes as we go along.

class SimpleDraw(Turtle):
    # No __init__ need, calls __init__ of subclass implicitly
    # So self refers to an instance of Turtle
    # Since all your code essentially makes use of the same thing, lets create
    # a generic base draw.
    def draw_generic(self, angle, length, n):
        for i in range(n):
            self.forward(length)
            self.left(angle)

    def draw_square(self, length):
        # Notice self instead of bob, this is how you refer to the instance
        # of the class that has been created.
        """
        This function draws a regular square.
        """
        self.draw_generic(90, length, 4) # Note, self implicitly passed

    def draw_polygon(self, n, length):
        """
        This function draws a regular polygon
        """
        angle = 360.0 / n
        self.draw_generic(angle, length, n)

    def draw_circle(self, t, r):
        circumference = 2 * math.pi * r
        n = 50
        length = circumference / n
        self.draw_polygon(t, n, length) # Note: draw_polygon instead of polygon


def test_simple_draw():
    # Note the lowercase and underscores, this preferred by PEP 8
    # http://stackoverflow.com/questions/8908760/should-i-use-camel-case-or-underscores-in-python
    options = ["1 - draw square", "2 - draw polygon", "3 - draw circle", "0 -exit"]
    msg = "Please choose a draw option:\n %s" % ("\n".join(options))
    # Note that number is a string, so we have to try and coerce it to an int, and
    # validate this as well.
    number = None
    while number is None:
        try:
            number = raw_input(msg)
            number = int(number)
            if 0 > number or 3 < number:
                raise ValueError # Not a valid option
        except ValueError: # If the coercion fails
            print "%s is not a valid option.  Please re-enter." % number
            number = None # Set to None so we loop again

    # Now we have the number, and it's actually a number.
    # You make a lot of use of else, which can be replaced by elif, which is the
    # like else but specifies a condition; a union of if and else
    drawObj = SimpleDraw() # We create our object here, note braces for instantiation
    if number == 0:
        exit()
    elif number == 1:
        drawObj.draw_square(5)
    elif number == 2:
        drawObj.draw_polygon(7, 70)
    else: # We can have confidence in this value because of our checks above
        drawObj.draw_circle(50, 50)


if __name__ == "__main__":
    test_simple_draw()

Now This code gets across the idea that you initially wanted, but makes use of the class inheritance that you were originally trying to use.
Further sources for reference:
Classes: http://docs.python.org/2/tutorial/classes.html

import math
import random
import turtle

class SimpleTurtle(turtle.Turtle):
    # There is no need to override the default __init__

    # Also, making the self argument 'bob' is just irritating -
    # everyone expects to see 'self', why mess us up?

    def draw_polygon(self, num_sides, side_length):
        """
        Draw an n-sided regular polygon
        """
        angle = 360. / num_sides
        for side in range(num_sides):
            self.forward(side_length)
            self.left(angle)

    def draw_square(self, side_length):
        """
        Draw a square
        """
        self.draw_polygon(4, side_length)

    def draw_circle(self, radius, num_sides=60):
        """
        Draw a circle
        """
        angle = 360. / num_sides
        side_length = 2. * radius * math.sin(math.radians(angle))
        self.draw_polygon(num_sides, side_length)

def get_int(prompt, lo=None, hi=None):
    while True:
        try:
            val = int(raw_input(prompt))
            if (lo is None or lo <= val) and (hi is None or val <= hi):
                return val
        except ValueError:
            pass

def do_menu(prompt, options):
    print(prompt)
    for i,opt in enumerate(options, 1):
        print(" {:>2}: {}".format(i, opt))
    return get_int("? ", 1, len(options)) - 1

def test_SimpleTurtle():
    bob = SimpleTurtle(shape="turtle")
    while True:
        choice = do_menu("Please choose a draw option:", ["draw a square", "draw a polygon", "draw a circle", "quit"])
        if choice == 0:
            side_length = random.randint(10, 50)
            bob.draw_square(side_length)
        elif choice == 1:
            sides = random.randint(5, 12)
            side_length = random.randint(6, 30)
            bob.draw_polygon(sides, side_length)
        elif choice == 2:
            radius = random.randint(8, 40)
            bob.draw_circle(radius)
        else:
            break

if __name__ == '__main__':
    test_SimpleTurtle()
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top